3

When setting false value to session, the session is set, but the value is empty. The type is boolean. This code:

<?php
    session_start();
    $_SESSION["IsMobile1"] = false;
    $_SESSION["IsMobile2"] = true;
    //header("location: ../../../index.php");
    echo "IsSet1: " . isset($_SESSION["IsMobile1"]) . "; IsMobile1: " . $_SESSION["IsMobile1"] . "; type: " . gettype($_SESSION["IsMobile1"]) . ";<br>";
    echo "IsSet2: " . isset($_SESSION["IsMobile2"]) . "; IsMobile2: " . $_SESSION["IsMobile2"] . ";<br>";
?>

prints out:

IsSet1: 1; IsMobile1: ; type: boolean;
IsSet2: 1; IsMobile2: 1;

My PHP version is 5.5.13. Is this expected behaviour? I am trying to read the session with code:

if (isset($_SESSION["IsMobile"]))
    {
        if (is_bool($_SESSION["IsMobile"]))
        {
            header("location: Mobile/");
            exit;
        }
    }

but of course it is not working, because IsMobile is empty boolean. (In original I just use IsMobile. IsMobile1 and IsMobile2 is just for testing).

4
  • is_bool tells you that you're dealing with a boolean. It doesn't check to see whether that variable is true or false. Commented Nov 17, 2014 at 21:11
  • 2
    U can't echo out false it's just nothing in PHP, same as with null Commented Nov 17, 2014 at 21:13
  • True. I meant filter_var not is_bool and filter_var('', FILTER_VALIDATE_BOOLEAN); return false which is expected behaviour. From: stackoverflow.com/questions/7336861/… Commented Nov 18, 2014 at 14:23
  • btw isset() will return false if the array index exists and has a value set, but the value is null. Alternatively you can use array_key_exists() if you just want to check if a key exists. If this code might run without the $_SESSION variable existing at all in some cases, you could combine the two by checking isset($_SESSION) before calling array_key_exists(). Commented Apr 10, 2020 at 18:07

3 Answers 3

8

I have faced this issue myself many times and, while still unconfirmed, I have come to this conclusion:

As boolean type can't be used as constant for 1 or 0, it just happens so that it is cast to int upon printing or any other output attempt. But it only happens to the TRUE. FALSE always (or at least in most cases) stays empty (I've never seen a bool var output FALSE).

But the good news in, if you use a variable defined as FALSE in an if statement, it will be interpreted as FALSE (as expected).

So, what to do if you want to output them?

Output strings:

if ($_SESSION["IsMobile"] == FALSE) {
    echo "FALSE";
}

UPDATE Oh yeah, here's what I missed in the docs:

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

The source

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

1 Comment

Not that the code is wrong, but just wanted to point out it does not actually check to see if the session variable is set to false, but checks to see if it is falsy since only == was used instead of ===. I'm wondering if PHP sessions are even capable of keeping data as a bool type across the session, or if at some point it would be converted to a string.
0

How about something like:

if (!empty($_SESSION["IsMobile"]))
{
  header("location: Mobile/");
  exit;
}

Comments

0

Try using the strict operator

$_SESSION['IsMobile'] === TRUE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.