oliverbj
oliverbj

Reputation: 6052

PHP - $_SESSION contains data, but is still NULL

I have a problem with my $_SESSION variable in PHP.

When I check what the session contains, this is the information I get:

var_dump($_SESSION);

gives me:

array(7) { ["hash"]=> string(26) "5523F85C0EC9F5523F85C0ECD9" ["numbersValue"]=> int(8) ["user"]=> array(3) { ["id"]=> string(5) "31871" ["username"]=> string(5) "Admin" ["logkey"]=> string(32) "b26e828f52844cb5ee2db5ce99470ee8" } ["addons"]=> array(0) { } ["load_plugins"]=> array(0) { } ["language"]=> string(7) "english" ["fb_Your Facebook App ID_state"]=> string(32) "ea6fffc683328ffb04324d0e27a7476d" } 

Although when I try to use a variable from the $_SESSION, like $_SESSION["username"]; which should say "Admin", all I get is NULL:

var_dump($_SESSION["username"]);

gives me:

NULL

This is the structure:

Why can't I use $_SESSION["DATA"] in my script?

Upvotes: 0

Views: 626

Answers (2)

AbraCadaver
AbraCadaver

Reputation: 78974

username is in the user array:

$_SESSION['user']['username']

You might use something like this to see the structure:

highlight_string(var_export($_SESSION, true));

Upvotes: 0

jeroen
jeroen

Reputation: 91734

You are not accessing your variables correctly:

var_dump($_SESSION["username"]);

As you can see in the var_dump of the session, you have a nested array, so you would need for this specific variable:

var_dump($_SESSION['user']["username"]);

Upvotes: 1

Related Questions