0

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?

jrbedard
  • 3,662
  • 5
  • 30
  • 34
Diamonte
  • 393
  • 7
  • 22
  • 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. – CodingInTheUK Jan 13 '17 at 12:26
  • Obvious first question! Thats script does have a `session_start()` at the top doesn't it – RiggsFolly Jan 13 '17 at 12:30
  • RTM http://php.net/manual/en/function.is-null.php – Funk Forty Niner Jan 13 '17 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... – Diamonte Jan 13 '17 at 12:39
  • did you just answer your own question? ^ – Funk Forty Niner Jan 13 '17 at 12:40
  • I think so, go ahead and take credit for it, I accept it if you do so because it works. – Diamonte Jan 13 '17 at 12:41
  • Actually @Diamonte that credit should go to you. Stack lets you post your own answer, which IMHO, you should. – Funk Forty Niner Jan 13 '17 at 12:44

4 Answers4

1

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().

hakre
  • 193,403
  • 52
  • 435
  • 836
0

Try this:

if(isset($_SESSION['guest']) && is_null($_SESSION['guest']))
{
    // your code
}
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
0
if($_SESSION['guest'] == "")
{
    // code
}
Sujal Patel
  • 2,444
  • 1
  • 19
  • 38
  • 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. – RBT Jan 14 '17 at 11:49
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
}
Manikiran
  • 2,618
  • 1
  • 23
  • 39