Search code examples
phpsessionphp-8

Why PHP Session is Empty Even After session_start()?


I just upgraded the existing version of Apache to 2.4 and MySQL to 8.0. After this upgrade I am facing a strange issue with PHP.

PHP $_SESSION remains an empty array even after calling session_start() function. Here is the code snippet from my PHP file.

session_start();

if(session_id() != ''){
    echo '<script>alert("session is active")</script>';
}
else{
    echo '<script>alert("'.json_encode($_SESSION).'")</script>';
}

enter image description here

Here you can see the javascript alert with empty array.

Did anyone ever face such issue?

another if condition check in file also returning true.

if ($token != $_SESSION[Codes::FORM_TOKEN_ELEMENT]) {
        header("location:index.php?msg=inft&auth=" . urlencode(CryptoJsAes::encryptData(0)));
        die;
    }

Here $token has value but $_SESSION[Codes::FORM_TOKEN_ELEMENT] does not.

I tried below code at the top of the page

if(!isset($_SESSION["ID"])) {
    echo "Session Started";
    session_start();
    $_SESSION["ID"] = rand(78273874,922839929928839);
}

!isset($_SESSION["ID"]) is always returning true


Solution

  • The issue is resolved after changing permissions on default session directory in /var/lib/php. I ran following command to resolve this issue.

    chmod -R 777 session