-1

我正在尝试为我网站上的每个页面创建会话变量检查。下面的代码很接近,但我只是不知道我是否正确构造了 If、elseif 和 else 语句。谢谢!

<?php

session_start();

if (isset($_SESSION['id']) && $_SESSION['administrator'] == 'YES'){
    session_write_close();
    echo "You are logged in & you are an administrator!";
    exit();
}   elseif (isset($_SESSION['id'])){
        session_write_close();
        echo "You are logged in BUT you are NOT an administrator!";
        exit();
    }   else {
    session_write_close();
    echo "You are NOT logged in!";
    exit();
}

?>
4

1 回答 1

1

您的代码将按预期工作。但是,您可以像下面这样编写它 - 它只是节省您编写id两次支票的时间。

if(isset($_SESSION['id']))
{
     if($_SESSION['administrator'] == 'YES')
     {
         // is admin
     }
     else
     {
         // logged in but not admin
     }
}
else
{
    // not logged in
}
于 2012-12-26T21:21:37.133 回答