Welcome Guest, Not a member yet? Register   Sign In
How to check CodeIgniter session variable ?
#1

[eluser]MrSmith[/eluser]
I use CodeIgniter session class to create session variables so in some web pages I want to check the session variable is created before display the content. Does CodeIgniter have function to check that ?
#2

[eluser]poji23[/eluser]
Hi,

If I'm not mistaken you could use:

Code:
$this->session->set_userdata();

and pass in your own array of data. Then you just check if it's empty or null.

Do consult the user guide it's very in-depth. Smile
#3

[eluser]oddman[/eluser]
MrSmith, I'd be doing something like:

Code:
if (!empty($this->session->userdata('session_variable')) { do this stuff }
#4

[eluser]MrSmith[/eluser]
Thank you very much for your helping :lol:
#5

[eluser]Michael Wales[/eluser]
The userdata() method will return FALSE if a value is not assigned so you can do a bit of optimizing (rather than calling a function, on a method, which then must do some type casting to determine if FALSE == empty() or not). You can just do this:
Code:
if ($this->session->userdata('session_var') === FALSE) {
  // Yay for optimization
} else {
  // We got us a session var
}
#6

[eluser]oddman[/eluser]
[quote author="Michael Wales" date="1211921511"]The userdata() method will return FALSE if a value is not assigned so you can do a bit of optimizing (rather than calling a function, on a method, which then must do some type casting to determine if FALSE == empty() or not). You can just do this:
Code:
if ($this->session->userdata('session_var') === FALSE) {
  // Yay for optimization
} else {
  // We got us a session var
}
[/quote]

ah very nice, Michael Wales educates me once again Big Grin
#7

[eluser]Aea[/eluser]
Here's how I do it...

Code:
if(!isset($this->session->userdata('session_var']){0})
{

}

or

Code:
if(!$this->session->userdata('session_var']))
{

}

I prefer the isset($var{0}) method because it will check if it's not only set, but if it actually contains data (rather then being blank).




Theme © iAndrew 2016 - Forum software by © MyBB