PHP anahtarı case sorun

2 Cevap php

Ben loggedon adında bir oturum değişkeni var. Onlar kapalı oturum açtığınızda kullanıcı oturum açmış ve 0 olduğunda ben 1 olarak ayarlayın.

Ben aşağıda görüldüğü gibi bir swtich açıklama yapmak. ama olması gerektiği gibi çalışmıyor.

    $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");
}

Herkes neden anlamak?

Alkış,

Jonesy

2 Cevap

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