Welcome Guest, Not a member yet? Register   Sign In
How to check session variable is empty ?
#11

[eluser]bigtony[/eluser]
[quote author="wiredesignz" date="1254233737"]You are both incorrect because you cannot run the isset() function on a function no matter what.[/quote]
Thanks, but I had already corrected my previous oversight (post #7). My point abut isset is correct though, as indicated by the following:
Code:
$x = FALSE;
if (isset($x)):
    print 'set';
else:
    print('not set');
endif;
The above will display 'set' even though $x is false, which is what you get from userdata if the session variable is not found.
#12

[eluser]suba[/eluser]
thanks...
great.
good explanation..
touch with me.
#13

[eluser]zee[/eluser]
Although its an old thread, I am under the same problem.
I am using the same method as shown in post#7 but still the session values show up as 'set'. I used both isset() and empty().
Code:
$logindata = $this->session->userdata('username');
if(isset($logindata)){
    echo "session is set";
}
else echo "session not set";

I tried to echo $logindata but it is blank. And I am using session in autoload (CI 2.1). Does that affect this behaviour?
As far as I think, $logindata is no more unset or empty as we have set it as $this->session->userdata('username') which is NULL. So it is no more unset/empty, but will echo null value and hence shows as blank.

So what is the solution to check if a session is set?
#14

[eluser]CroNiX[/eluser]
Code:
$logindata = $this->session->userdata('username');

if ($logindata !== FALSE)
{
  //it exists
}
Same with CI's input methods and just about everything else (returns boolean FALSE if it doesn't exist). Although they will probably be changing it to NULL in CI3 instead of false.

This is really super easy to figure out if you just look at their code in addition to the docs:

From session.php:
Code:
function userdata($item)
{
  return ( ! isset($this->userdata[$item])) ? FALSE : $this->userdata[$item];
}




Theme © iAndrew 2016 - Forum software by © MyBB