如何使用登录使用2文件设置会话?首页>登录>首页
我在代码中做错了什么? 谢谢。如何使用登录使用2文件设置会话?主页>登录>首页
home.php
<?php
session_start();
?>
<!DOCTYPE html>
<html><head><title></title></head>
<body>
<?php
if($_SESSION['account']){
print"login successful";
//do something
}
else{
print"login invalid";
print"
<form method=\"post\" action=\"login.php\">
account: <input type=\"text\" name=\"account\"><br>
password: <input type=\"text\" name=\"password\"><br>
<input type=\"submit\" value=\"login\">
</form>
";
}
?>
</body>
</html>
的login.php
$account = mysql_escape_string($_POST['account']);
$password = mysql_real_escape_string($_POST['password']);
if($account == 'myaccount' && $password == 'mypassword'){
session_start();
$_SESSION['account'] = $account;
$_SESSION['password'];
print"<meta http-equiv=\"refresh\" content=\"0;url=home.php\">";
header("location: home.php");
exit();
}
else{
print"<meta http-equiv=\"refresh\" content=\"0;url=home.php\">";
header("location: home.php");
exit();
}
感谢答复,但之后,我提出,它仍然会显示invaild(在else),我不明白为什么? – user1775888
尝试在login.php中删除'print',并且只留下'header()'。无论哪种方式,标题将在响应开始时发送,因此,除非您正在缓冲,否则在输出任何内容后调用'header()'将成为无操作。 –