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

8

5

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?

Barca answered 20/3, 2012 at 6:17 Comment(3)
Did you remember session_start(); ? Although it might seem trivial, you should make sure.Bekki
Yup, the session is started in the constructor of a parent class. die(session_id) returns a string as intended.Barca
Use var_dump instead of die or echo. In PHP a boolean false that is converted to string (in your case for output) will be converted to an empty string. var_dump will show you the actual value and type. That is just basic PHP, please make yourself comfortable with the language first.Quito
E
8

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.

Explorer answered 20/3, 2012 at 6:19 Comment(6)
public function __isset($var) { return isset($_SESSION[$var]); } public function isItSet($var) { die(isset($_SESSION[$var])); } Thanks for the quick response, but I'm still having the same problem with the above code. I also tried isset($this->session->idUser)Barca
You can remove the isItSet() function as it is not of any use. I have also updated my answer.Explorer
isset($this->session->var) still returns nothing. I got rid of isItSet(). var_dump shows that $_SESSION is an empty array. (Which is expected, so shouldn't the isset return false?)Barca
Just to make sure... __isset() is only supported from php 5.1.0, what's your php version?Vannessavanni
have you tried var_dump($this->session->var)? I find that most of the time echoing a boolean value does not output anything.Explorer
That's good. What about var_dump(isset($this->session->var))?Explorer
B
3

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.

https://www.php.net/die

So, use var_dump() instead.

Busk answered 1/4, 2014 at 7:8 Comment(0)
S
1

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'])));

Sardella answered 26/3, 2014 at 21:56 Comment(0)
T
1

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 ""
Timelag answered 2/4, 2014 at 7:36 Comment(0)
A
0

The isset function: Returns TRUE if var exists and has value other than NULL, FALSE otherwise. if the session variable is set gives TRUE if the session variable unset it gives FALSE.

Example:

$_SESSION['views']=1; session variable is setted. => gives TRUE

unset($_SESSION['views']); session variable is not setted. => after this if(isset($_SESSION[$var])) it gives FALSE

There are two options: the session variable is setted or not.

isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.

For More Information about isset click here

Alika answered 26/3, 2014 at 13:32 Comment(0)
B
0

A couple of things you could try:

Add this to your codebase: http://philsturgeon.co.uk/blog/2010/09/power-dump-php-applications. It's much better than var_dump and you can clearly see true or false for boolean values. It's saved me much grief.

Second, die() doesn't always give me what I expected, so it's better to echo something and then die() right after.

As @doydoy44 mentioned, false outputs a blank, so you might be getting a blank page for good reason. Again, the link above will solve this problem and make things clearer.

Your original code above has idUser as the variable being checked, all your other examples use id. I assume you know that, but I thought I'd mention it just in case.

Finally, I'd cross-check that the session wrapper was working as I'd expected, e.g.

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

I'd obviously expect to get the same content back from both of them.

Hope that helps somewhat.

Billion answered 27/3, 2014 at 9:34 Comment(0)
M
0

By the looks of it, it seems that there is no "ID" key on the session array. This was verified on your comment to F21's answer ("undefined index error").

What you can do is define/initialize an "ID" key on the session array with value as null or empty string "" after your session has started. Then populate it with your value later on with your codes.

This is to make sure that when you use isset($_SESSION["ID"], the result can either be true or false rather than an undefined index error.

Metagenesis answered 2/4, 2014 at 3:59 Comment(0)
S
0

Session variables are a bit magic. Less magic than historically, but still it's unsurprising that unusual things are observed.

It would make sense to run the tests in the question in three states: once with the session variable set, and twice without the session variable set - once with a different session variable set and once with no session variables set.

Adapting F21's answer, to support isset() you need to overload the function in your wrapper. Whilst doing that you may as well create an isset that functions as you expect.

Something along the lines of the following will solve both the 'false is invisible' and an issue with removing session values.

public function __isset($var){
 if (isset($_SESSION[$var]) && $_SESSION[$var]."">"") {
  return true;
 } else {
  return false;
 }
}
Sunshade answered 2/4, 2014 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.