views:

140

answers:

2

hey folks,

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

A: 
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...

michael
+1  A: 

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();

Lord Otori