1

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

4
  • 3
    How do you mean it's not working as it should? Have you initializes the session before using session variables?
    – RaYell
    Commented Jun 4, 2010 at 11:51
  • what is not working? do you get an error or nothing at all?
    – Thariama
    Commented Jun 4, 2010 at 11:51
  • @RaYell thanks I totally forgot I had to session_start(); cheers!
    – iamjonesy
    Commented Jun 4, 2010 at 11:56
  • What is it doing that you don't expect?
    – Jeff Beck
    Commented Jun 4, 2010 at 11:57

2 Answers 2

1

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

0
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...

Not the answer you're looking for? Browse other questions tagged or ask your own question.