Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP isset($_SESSION[$var]) Not working at all

Tags:

php

session

isset

In my project, I have a wrapper class for $_SESSION, so session variables can be accessed by $session->var. I was trying to check if session variables were set or not using isset:

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

but isset is not returning anything. So in my wrapper class I wrote the following function to test what was going on.

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

this results in an empty page. I tried:

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

still an empty page. Then I tried:

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

This returns 1.

Can anyone tell me why trying to check if a session variable is set returns nothing?

like image 449
Jormundir Avatar asked Mar 20 '12 06:03

Jormundir


People also ask

What is isset ($_ session in PHP?

isset is a function that takes any variable you want to use and checks to see if it has been set. That is, it has already been assigned a value. With our previous example, we can create a very simple pageview counter by using isset to check if the pageview variable has already been created.

How do I check if a session variable has a value?

You can check whether a variable has been set in a user's session using the function isset(), as you would a normal variable. Because the $_SESSION superglobal is only initialised once session_start() has been called, you need to call session_start() before using isset() on a session variable.

How can I access session variable in PHP?

Get PHP Session Variable Values From this page, we will access the session information we set on the first page ("demo_session1.php"). Notice that session variables are not passed individually to each new page, instead they are retrieved from the session we open at the beginning of each page ( session_start() ).

How do I check if a session variable is empty?

If you want to check whether sessions are available, you probably want to use the session_id() function: session_id() returns the session id for the current session or the empty string ("") if there is no current session (no current session id exists).


4 Answers

To support isset() you need to overload the function in your wrapper.

So, in your wrapper, add:

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

To use it, you just have to do:

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

If it is still not working, do:

var_dump($_SESSION)

This will dump the whole $_SESSION array and show you whether the variable you are checking for actually exists.

like image 154
F21 Avatar answered Oct 06 '22 22:10

F21


In my opinion, issue is related to testing and not with PHP session.

The MAIN REASON behind why session variable isset returns nothing is use of die() function which is equivalent to exit() according to PHP manual itself.

http://in1.php.net/die

So, use var_dump() instead.

like image 24
dikesh Avatar answered Oct 07 '22 00:10

dikesh


Your results are basically correct.

The boolean values being pass to each function just confused you i think.

Heres a clear explainations what happens to each of your statement.

LETS GET IT ONE BY ONE

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

note: In general your statement is correct just a few justification is needed to support it.


Additional:

print_r(),var_dump()

  • use it to check your session if it has a name that youre trying to declared.

A brief explanation:

isset()

  • returns a boolean value of true or false

is_null()

  • accepts only a NULL parameter value to return a boolean value of true else it goes false.

die()

  • returns a value of 1 for true and empty value for false.

Reasons:

  • A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to ""
like image 44
Jhonathan H. Avatar answered Oct 06 '22 23:10

Jhonathan H.


isset returns an empty page because your variable does not exist in session, so isset return false, however, echo false return ''.
It was already written in the commentary to hakre

You can test this like that:

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

=>

false : 
true : 1

So, for your test, do a var_dump($_SESSION) and you will see if it is normal that isset($_SESSION['id']) returns false.

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

like image 39
doydoy44 Avatar answered Oct 06 '22 22:10

doydoy44