I have a session variable called loggedon. I set it to 1 when the user is logged on and 0 when they are logged off.
I do a swtich statement as seen below. but it isn't working as it should.
$state = $_SESSION['loggedon'];
switch ($state)
{
case 0:
include("../includes/login.php");
break;
case 1:
echo "logged in";
echo "<br /><a href='../logoff.php'>Log off</a>";
break;
default:
include("../includes/login.php");
}
Anyone understand why?
Cheers,
Jonesy
If the contents of the $_SESSION['loggedon'] var are just 0/1 you can use the if statement (PHP reads 1 as true and the rest of the numbers as false) which will work a bit faster. Just do the following:
if($_SESSION['loggedon']){
echo "logged in";
echo "<br /><a href='../logoff.php'>Log off</a>";
}else{
include("../includes/login.php");
}
Remember you are required to session_start();
switch ($_SESSION['loggedon'])
{
case 1:
echo "logged in";
echo "<br /><a href='../logoff.php'>Log off</a>";
break;
default:
include("../includes/login.php");
}
Is less verbose and should do the same :-) Sorry for the code not being formatted as code...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.