ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Session FALSE if not exists - how do i check this?

April 06, 2010 5:23pm

Subscribe [2]
  • #1 / Apr 06, 2010 5:23pm

    Neil Evans

    1403 posts

    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?

    $value = FALSE;
    if (isset($value) && $value !== FALSE) { echo "true"; } else { echo "false"; }
  • #2 / Apr 06, 2010 5:53pm

    danmontgomery

    1802 posts

    If you’re using the session library,

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

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

    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.

  • #3 / Apr 06, 2010 5:58pm

    Neil Evans

    1403 posts

    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:

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

  • #4 / Apr 06, 2010 6:08pm

    Bart v B

    227 posts

    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:

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

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

    // 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"; 
       }
  • #5 / Apr 06, 2010 6:12pm

    Neil Evans

    1403 posts

    Ahhhhh balls… i am with you… stupid typo and missed characters on my behalf.
    Thanks for pointing out the obvious to the blind!!! 😊

  • #6 / Apr 06, 2010 6:38pm

    Bart v B

    227 posts

    Ahhhhh balls… i am with you… stupid typo and missed characters on my behalf.
    Thanks for pointing out the obvious to the blind!!! 😊

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

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases