CodeIgniter Forums
Session FALSE if not exists - how do i check this? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Session FALSE if not exists - how do i check this? (/showthread.php?tid=29336)



Session FALSE if not exists - how do i check this? - El Forum - 04-06-2010

[eluser]nevsie[/eluser]
Hi All,
If i am checking a session for a value, and it does not exist... It logically returns boolean FALSE.

The problem i have is how do i check this???
if i check with an isset() - it detects it as a true...
if i check for != FALSE it throws a PHP notice for undefined index...

the only logical way to check this is by check both isset() && != FALSE like below... but this seems like overkill for something that should be clean cut... can anyone advise?

Code:
$value = FALSE;
if (isset($value) && $value !== FALSE) { echo "true"; } else { echo "false"; }



Session FALSE if not exists - how do i check this? - El Forum - 04-06-2010

[eluser]danmontgomery[/eluser]
If you're using the session library,

Code:
$this->session->userdata('my_value');

Will return the value if it exists, or false if it doesn't.

Code:
if($this->session->userdata('my_value') !== FALSE) {

If you're not using the session library, and are checking a normal variable like $value, then what you have is technically correct.


Session FALSE if not exists - how do i check this? - El Forum - 04-06-2010

[eluser]nevsie[/eluser]
Hi Noctrum,
That is kind of my point... I am using the session library, and if the item does not exist it throws up the error as mentioned above:

Code:
// Monkey does not exist in session!!!
if ($this->session->userdata['monkey'] !== FALSE) { echo "false is stated"; } else { echo "true is stated"; }

And the error:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: monkey

I know this gives the correct answer and with error reporting off this will be suppressed - but still not ideal...


Session FALSE if not exists - how do i check this? - El Forum - 04-06-2010

[eluser]Bart v B[/eluser]
[quote author="nevsie" date="1270605501"]Hi Noctrum,
That is kind of my point... I am using the session library, and if the item does not exist it throws up the error as mentioned above:

Code:
// Monkey does not exist in session!!!
if ($this->session->userdata['monkey'] !== FALSE) { echo "false is stated"; } else { echo "true is stated"; }

And the error:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: monkey

I know this gives the correct answer and with error reporting off this will be suppressed - but still not ideal...[/quote]

Offcourse it gives an undefined index.
It must be something like this:

Code:
// Monkey does not exist in session!!! mind the () instead of: []
if ($this->session->userdata('monkey') === FALSE)
{
  echo "false is stated";
}
else // its true so..
   {
    echo "true is stated";
   }



Session FALSE if not exists - how do i check this? - El Forum - 04-06-2010

[eluser]nevsie[/eluser]
Ahhhhh balls... i am with you... stupid typo and missed characters on my behalf.
Thanks for pointing out the obvious to the blind!!! :-)


Session FALSE if not exists - how do i check this? - El Forum - 04-06-2010

[eluser]Bart v B[/eluser]
[quote author="nevsie" date="1270606363"]Ahhhhh balls... i am with you... stupid typo and missed characters on my behalf.
Thanks for pointing out the obvious to the blind!!! :-)[/quote]

no problem...
With 6 pear of eyes you see more then just two.