Welcome Guest, Not a member yet? Register   Sign In
How to check if a session already been set or not ?
#1

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

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

[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.
#4

[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-gui...sions.html

Cheers, Michiel
#5

[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.
#6

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

[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...
#8

[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';
}
#9

[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';
}
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB