2

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

Makla
  • 9,899
  • 16
  • 72
  • 142
  • `is_bool` tells you that you're dealing with a boolean. It doesn't check to see whether that variable is true or false. – andrewsi Nov 17 '14 at 21:11
  • 2
    U can't echo out `false` it's just nothing in PHP, same as with null – DarkBee Nov 17 '14 at 21:13
  • True. I meant filter_var not is_bool and filter_var('', FILTER_VALIDATE_BOOLEAN); return false which is expected behaviour. From: http://stackoverflow.com/questions/7336861/how-to-convert-string-to-boolean-php – Makla Nov 18 '14 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()`. – still_dreaming_1 Apr 10 '20 at 18:07

3 Answers3

7

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

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
  • 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. – still_dreaming_1 Apr 10 '20 at 18:03
0

How about something like:

if (!empty($_SESSION["IsMobile"]))
{
  header("location: Mobile/");
  exit;
}
ulentini
  • 2,413
  • 1
  • 14
  • 26
0

Try using the strict operator

$_SESSION['IsMobile'] === TRUE
Tejas Jadhav
  • 693
  • 2
  • 10
  • 11