1

How I can check in PHP if SESSION exists but is null.

I have really old self made platform, where visitor can log in as "guest", it sets $_SESSION "guest" but it is NULL.

Now I wan't to show some specific content to only registered. If I check isset($_SESSION['guest']), is_null($_SESSION['guest']) or is_null($_SESSION['guest'] === FALSE they all return same.

How I can check if guest session exists but is null?

7
  • All of those are meant to return the same, they operate on boolean values 1,0 true, false. You would be better to be checking if there is an active session instead, just as you would to check the user is logged in as a registered user instead, you could add a new element to the session "registered" with a true or false value assigned to it then check that. Commented Jan 13, 2017 at 12:26
  • Obvious first question! Thats script does have a session_start() at the top doesn't it Commented Jan 13, 2017 at 12:30
  • RTM php.net/manual/en/function.is-null.php Commented Jan 13, 2017 at 12:33
  • yes, sessions are set of course. the sessions guest exists BUT AS NULL. so I found a way to find if sessions[key] exists. foreach($_SESSION as $key => $val) { if( $key == 'guest') $guest = true; } that way I know if there is key "guest" even if its null... after thinking about this, I feel so dumb, it wasn't never about the value but key... I wanna whack my head to wall... Commented Jan 13, 2017 at 12:39
  • did you just answer your own question? ^ Commented Jan 13, 2017 at 12:40

4 Answers 4

2

This question was not yet really answered (albeit they found a way to foreach over the array to look-up the 'guest' key in the $_SESSION array).


The problem is to check an array entry that is null. Reason is that a check with isset($_SESSION['guest']) returns false as the member is null. Cf. https://php.net/isset .

A function that works is array_key_exists(), as it takes all members into account, including those which are null. It really tests if the key exists in the array.

Example for the case that the guest member is null:

$_SESSION['guest']; # null, no warning
isset($_SESSION['guest']); # false
array_key_exists('guest', $_SESSION); # true

Naturally, using foreach works as well, but it requires more code and the functionality already exists with the array_key_exists() function.

Reference: https://php.net/array_key_exists – see especially Example #2 array_key_exists() vs isset().

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

if(isset($_SESSION['guest']) && is_null($_SESSION['guest']))
{
    // your code
}

Comments

0
if($_SESSION['guest'] == "")
{
    // code
}

1 Comment

May I request you to please add some more context around your answer. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post.
0

You can try using empty instead, for example:

session_start();
if(isset($_SESSION['guest']) && !empty($_SESSION['guest'])) {
    //Session is not null or empty
}
else
{
    //Session is null or empty
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.