我試圖設置一個簡單的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日誌。 –