我写了这个php脚本:
<?
session_start();
if(isset($_GET["logout"])) {
unset($_SESSION["user"]);
unset($_SESSION["user_token"]);
echo "successful logout";
die;
}
if(!isset($_SESSION["user_token"])){
$GETusername = $_SERVER["PHP_AUTH_USER"];
$GETpassword = $_SERVER["PHP_AUTH_PW"];
$users = array(
'user' => 'pass',
'user2' => 'pass2',
'user3' => 'pass3'
);
$user = isset($GETusername) ? strtolower($GETusername) : '';
$pass = isset($GETpassword) ? $GETpassword : '';
$config["admin_username"] = $user;
$config["admin_passowrd"] = $pass;
header("WWW-Authenticate: Basic realm=\"Login\"");
if (!isset($users[$user]) or $users[$user] != $pass) {
?>
wrong username or password!
<?
die;
}
$_SESSION["user"] = $config["admin_username"];
$_SESSION["user_token"] =
"auth-".md5($config["admin_username"]).md5($config["admin_passowrd"]);
die;
}
echo "username: ".$_SESSION["user"]."<br /><a href='?logout'>logout</a>";
?>
当我尝试登录第一次时,我必须输入我的用户名和密码两次(就好像我键入了错误的通行证或用户名)!但是当我退出并尝试再次登录时,一切正常!问题在哪里?
答案 0 :(得分:0)
在您的脚本上方添加session_start
session_start();
答案 1 :(得分:0)
问题解决了! 我真的不知道怎么做,但它现在有用了!