我试图设置一个简单的PHP登录脚本,在成功登录后重定向。问题是标题调用只有在成功登录后手动刷新后才有效。对于我在SO上发现的这个问题,唯一的答案是“不要在呼叫头部之前回应内容”,但是我不相信我。我确实读过session_start发送头文件,但如果我在调用isset之后移动它,页面在刷新之后不会重定向,导致我相信会话变量未设置。登录后重定向PHP
<?php session_start();
if(isset($_SESSION['logged']))
{
header("Location: index.html");
exit;
}
function BuildLogin(){
include("passwords.php");
if ($_POST["ac"]=="log") { /// do after login form is submitted
if ($USERS[$_POST["username"]]==$_POST["password"]) {
$_SESSION["logged"]=$_POST["username"];
} else {
echo 'Incorrect username/password. Please, try again.';
};
};
if (!array_key_exists($_SESSION["logged"],$USERS)) {
echo '<form action="login.php" method="post"><input type="hidden" name="ac" value="log"> ';
echo 'Username: <input type="text" name="username" />';
echo 'Password: <input type="password" name="password" />';
echo '<input type="submit" value="Login" />';
echo '</form>';
};
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" http-equiv="x-ua-compatible" content="IE=9">
<title>File Management</title>
<style type="text/css">/* IE has layout issues when sorting (see #5413) */
.group { zoom: 1 }
body{
padding:0px 25px 25px 25px;
}
#content {
background-color:#989898;
box-shadow: 0px 10px 20px rgba(0,0,0,.5);
border-top-right-radius: 4px;
border-top-left-radius: 4px;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
padding:25px 25px 25px 25px;
height:auto;
overflow:auto;
}
</style>
</head>
<body>
<div id="content">
<?php BuildLogin(); ?>
</div>
</body>
</html>
感谢TheBeacon的解决方案。这里是PHP的版本更新
<?php
session_start();
if (isset($_POST["ac"]) && !empty($_POST) && $_POST["ac"]=="log") {
include("passwords.php");
if ($USERS[$_POST["username"]]==$_POST["password"]) {
$_SESSION["logged"]=$_POST["username"];
header('Location: index.html');
};
};
function BuildLogin(){
include("passwords.php");
if (isset($_POST["ac"]) && !empty($_POST) && $_POST["ac"]=="log") {
echo 'Incorrect username/password. Please, try again.';
};
if (!array_key_exists($_SESSION["logged"],$USERS)) {
echo '<form action="login.php" method="post"><input type="hidden" name="ac" value="log"> ';
echo 'Username: <input type="text" name="username" />';
echo 'Password: <input type="password" name="password" />';
echo '<input type="submit" value="Login" />';
echo '</form>';
};
}
?>
您的PHP日志中是否有任何内容? –
对不起,我是PHP新手。我如何审核他们? – rmsanger
取决于您的操作系统。只需谷歌你的操作系统和PHP日志。 –