What is the proper way to test CodeIgniter session variable?
Solution 1
Try doing the following instead:
<?php
$loggedIn = 0;
if ($this->session->userdata('userID') !== FALSE) {
$loggedIn = 1;
}
?>
If the error continues, you'll need to post more code in case you're calling that variable in another scope.
Solution 2
If your aim is to see whether or not the session variable 'userID'
is set, then the following should work:
$this->session->userdata('userID') !== false
![JamieHoward](https://i.stack.imgur.com/Tba4W.jpg?s=256&g=1)
JamieHoward
Spend most of my time in PHP (Laravel framework mostly). Self-taught, so StackOverflow is a big source of my learning.
Updated on June 08, 2022Comments
-
JamieHoward about 2 years
Take the following code snippet. What is the best way to test to make sure the session variable isn't empty?
<?php if ($this->session->userdata('userID')) { $loggedIn = 1; } else { $loggedIn = 0; } ?>
If later in my script, I call the following, the first prints properly, but on the second I receive Message: Undefined variable: loggedIn
<?php echo $this->session->userdata('userID')); echo $loggedIn; ?>
I've tried using
!empty
andisset
, but both have been unsuccessful. I also tried doing the if/then statement backwards usingif (!($this->session->userdata('userID'))
, but no dice. Any thoughts? -
JamieHoward about 12 yearsIt actually ended up being a problem with not passing the variables to the view I was loading (noob move!), but I appreciate the clarification anyway. Thanks friend!