How to check if a session already been set or not ? |
[eluser]Teks[/eluser]
My small brain cannot make sense of this question... When would a session NOT be set?
[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.
[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"; Reference: http://ellislab.com/codeigniter/user-gui...sions.html Cheers, Michiel
[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.
[eluser]Eric Cope[/eluser]
If you know a session variable, you can use isset() Code: isset($this->session->userdata('variable')...
[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...
[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')){
[eluser]Michael Wales[/eluser]
What if 'variable' was set to 0 in this example? Quote: Always, always, ALWAYS, always, ALWAYS use strict comparisons: Code: if ($this->session->userdata('variable') !== FALSE) {
|
Welcome Guest, Not a member yet? Register Sign In |