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).
is_booltells you that you're dealing with a boolean. It doesn't check to see whether that variable is true or false.falseit's just nothing in PHP, same as with nullisset()will return false if the array index exists and has a value set, but the value is null. Alternatively you can usearray_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 checkingisset($_SESSION)before callingarray_key_exists().