PHP isset ($ _ SESSION [$ var]) Doesn't work at all

In my project, I have a wrapper class for $_SESSION, so I can access session variables with $session->var. I tried to check if session variables were set or not using isset:

if (!isset($this->session->idUser)) {
    ...
}

but issetreturns nothing. Therefore, in my wrapper class, I wrote the following function to check what happens.

public function isItSet($var)
{
   die(isset($_SESSION["id"]));
}

this results in a blank page. I tried:

public function isItSet($var)
{
    die(is_null(isset($_SESSION["id"])));
}

still blank page. Then I tried:

public function isItSet($var)
{
    die(isset($_SESSION));
}

This returns 1.

Can someone tell me why trying to check if the session variable is set does not return anything?

+5
source share
8 answers

isset() .

, :

public function __isset($var){
  return isset($_SESSION[$var]);
}

, :

isset($this->session->myVar);

, :

var_dump($_SESSION)

$_SESSION , , .

+8

, , PHP.

session variable isset , () exit() PHP.

http://in1.php.net/die

, var_dump().

+3

isset , , isset false, echo false return ''.
hakre

:

echo "false : " . false . "\n";
echo "true : " . true . "\n";

= >

false : 
true : 1

, var_dump($_SESSION), , , isset($_SESSION['id']) false.

: die("false : " . (false == isset($_SESSION['id'])));

+1

.

, , , .

, .

// declaring this array as a set of session being passed as sample.
$string = array('name'=>'kaii');

echo die(isset($string['id'])); // figure 1
// step
// isset($string) returns a value of false because of unexistent of an array with the name of id.
// die(false)   returns nothing because of the value being passed by isset is false.
// result would be definitely empty

echo die(is_null(isset($string['id']))); // figure 2
// step
// isset($string) returns a value of false because of unexistent of an array with the name of id.
// is_null(false) returns a value of false because it only accepts NULL as its parameter value to return true.
// die(false)   returns nothing because of the value being passed by is_null is false.
// result would be definitely empty


echo die(isset($string)); //figure 3
// step
// isset($string) returns a value of true because of a $string variable is set and exist.
// die(true)    returns a value of 1 because of the value being passed is true which is equivalent to 1 .
// result would be definitely return a value of 1.

: , .


:

print_r(), var_dump()

  • , , , .

:

isset()

  • true false

is_null()

  • NULL true false.

die()

  • 1 true false.

:

  • boolean true "1". false ""
+1

isset: TRUE, var, , NULL, FALSE . TRUE, , FALSE.

:

$_SESSION['views']=1; . = > TRUE

unset($_SESSION['views']); . = > if(isset($_SESSION[$var])) FALSE

: .

isset() , - . , , () .

isset

0

, :

: http://philsturgeon.co.uk/blog/2010/09/power-dump-php-applications. , var_dump, true false . .

-, die() , , echo -, die() .

@doydoy44, false , . , .

idUser id. , , , .

, , , , .

if (!isset($this->session->idUser)) {
    dump($_SESSION);
    dump($this->session);
}

, , .

, .

0

, , "ID" . F21 ( "undefined " ).

, / "ID" "" . .

, isset ($ _ SESSION [ "ID" ] true, false, undefined.

0

. , , , .

: - .

Adapting the F21 answer to support isset (), you need to overload the function in your wrapper. By doing this, you can also create an isset that functions as you expect.

Something along the lines of the following will solve both "false is invisible" and the problem of deleting session values.

public function __isset($var){
 if (isset($_SESSION[$var]) && $_SESSION[$var]."">"") {
  return true;
 } else {
  return false;
 }
}
0
source

All Articles