CodeIgniter Forums
How to check if a session already been set or not ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to check if a session already been set or not ? (/showthread.php?tid=14491)

Pages: 1 2


How to check if a session already been set or not ? - El Forum - 01-04-2009

[eluser]RaiNnTeaRs[/eluser]
base on the title


How to check if a session already been set or not ? - El Forum - 01-05-2009

[eluser]Teks[/eluser]
My small brain cannot make sense of this question... When would a session NOT be set?


How to check if a session already been set or not ? - El Forum - 01-08-2009

[eluser]RaiNnTeaRs[/eluser]
I'm sorry, what i mean is, how can i know if i have/haven't set a new session variable ( with $this->session->set_userdata(); ) command.


How to check if a session already been set or not ? - El Forum - 01-08-2009

[eluser]mvdg27[/eluser]
I would say: try to fetch data from the session. If it returns something other than false, it must be set:

Code:
if( $this->session->userdata('session_id') ) print "yes the session has been set";
else print "no the session hasn't been set";

Reference: http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

Cheers, Michiel


How to check if a session already been set or not ? - El Forum - 01-08-2009

[eluser]Chris Williams[/eluser]
Are you talking about expiring sessions? I had an issue where my "cart" was using the session value to track items. My cart id was a part of the session data and I used that to compare value changes.


How to check if a session already been set or not ? - El Forum - 01-08-2009

[eluser]Eric Cope[/eluser]
If you know a session variable, you can use isset()
Code:
isset($this->session->userdata('variable')...



How to check if a session already been set or not ? - El Forum - 01-08-2009

[eluser]RaiNnTeaRs[/eluser]
thx for all the idea guyz, but i think i have tried to used the isset() function back then, and it didnt work...


How to check if a session already been set or not ? - El Forum - 01-09-2009

[eluser]darkhouse[/eluser]
yeah, you can't use isset() on a function, $this->session->userdata('variable') is a function that returns a value or false if it's not set, so mvdg27's solution is how you do it:

Code:
if($this->session->userdata('variable')){
     echo 'variable is set';
} else {
     echo 'variable is NOT set';
}



How to check if a session already been set or not ? - El Forum - 01-09-2009

[eluser]Michael Wales[/eluser]
What if 'variable' was set to 0 in this example?
Quote:
Code:
if($this->session->userdata('variable')){
     echo 'variable is set';
} else {
     echo 'variable is NOT set';
}


Always, always, ALWAYS, always, ALWAYS use strict comparisons:
Code:
if ($this->session->userdata('variable') !== FALSE) {
  echo 'Variable is set';
} else {
  echo 'Variable is not set';
}



How to check if a session already been set or not ? - El Forum - 01-09-2009

[eluser]darkhouse[/eluser]
True, bad me.