I'm encountering an issue with sessions that I can't figure out. I have a class that loads at the start of index.php. (Below code is an excerpt, but is the first code executed). If the user logs out, I try to destroy the session, at which point I get a "trying to destroy uninitialized session after session_start" error. If the user does not logout, I access session variables with no problem. In trying to determine why, I added code to check session_status and found PHP_SESSION_ACTIVE immediately after session_start(), but when I again check session_status within the unsetSession function, right before session_destroy(), I find the session is not active. How is that possible?
class UserAccess
{
public function __construct($page)
{
session_start();
if ($page == 'logout') {
$this->logout();
} else {
$this->checkSession();
}
}
private function unsetSession()
{
session_destroy();
}
private function logout()
{
$this->unsetSession();
}
Index.php:
<?php
define ('BASE_DIR', __DIR__ . '/');
include("includes/config.php");
include(INCLUDE_DIR . 'autoload.inc.php');
//** Get Target Page
$current_page_uri = HTMLSPECIALCHARS($_SERVER['REQUEST_URI']);
$path = parse_url($current_page_uri, PHP_URL_PATH);
$part_url = explode("/", $path);
$page = end($part_url);
$DB = DB::dbConnection();
$UserAccess = new UserAccess($page);