这是我的代码......我无法弄清楚为什么一直在说
Warning: Cannot modify header information - headers already sent by (output started at /opt/apache/htdocs/save/header.php:10) in /opt/apache/htdocs/****/attendance.php on line 41
Warning: Cannot modify header information - headers already sent by (output started at /opt/apache/htdocs/save/header.php:10) in /opt/apache/htdocs/****/attendance.php on line 42
我只想让登录页面正常工作。我还想将$ _SERVER [PHP_SELF()]更改为实际的url,以便我的css等仍可正常工作。
include('header.php');
$server = "serverName";
$dUsername = "username";
$dPass = "password";
$username = "username2";
$password = "password2";
$randomword = "randomword";
if(isset($_COOKIE['MyLoginPage'])) {
if($_COOKIE['MyLoginPage'] == md5($password.$randomword)) {
$conn = mysql_connect($server, $dUsername, $dPass) or die("error connecting to MySQL database");
mysql_select_db("w3_save", $conn);
$query = "SELECT DISTINCT MemberName FROM attendance;";
$result = mysql_query($query, $conn) or die(mysql_error());
echo "<div id='rightColumn'><div id='title'><h1>Attendance</h1></div><div id='content'>";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "Name: $row[MemberName] <br />";
}
echo "</div></div></body></html>";
mysql_close($conn);
exit;
}
else {
echo "<p>Bad Cookie. Please clear them out and try again</p>";
exit;
}
}
if(isset($_GET['p']) && $_GET['p'] == 'login') {
if($_POST['name'] != $username) {
echo "<p>Sorry the username you entered is incorrect.</p>";
exit;
}
else if($_POST['pass'] != $password) {
echo "<p>Sorry the password you entered is incorrect.</p>";
exit;
}
else if($_POST['name'] == $username && $_POST['pass'] == $password) {
**setcookie('MyLoginPage', md5($_POST['pass'].$randomword)); LINE 41
header("Location: $_SERVER[PHP_SELF]");** LINE 42
} else {
echo "<p>Sorry you could not be logged in at this time please try again</p>";
}
}
<div id='rightColumn'>
<div id='title'>
<h1>Attendance</h1>
</div>
<div id='content'>
<form action="``<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method='post'>
<label>Username: <input type='text' name='name' id='name' /></label><br />
<label>Password: <input type='password' name='pass' id='pass' /></label><br />
<input type='submit' id='submit' value='Login' />
</form>
</div>
</div>
</body>
</html>
答案 0 :(得分:3)
如评论中所述,错误消息非常自我解释。显然header.php
正在开始输出。你应该追踪它并弄清楚原因。解决方法是使用输出缓冲:
ob_start();
include('header.php');
...
ob_end_flush();
?>
这将阻止在调用flush函数之前发送任何输出; Documenation
关于$_SERVER['PHP_SELF']
:你不应该编辑超级全局。在这种情况下,您所追求的信息使用不同的索引:$_SERVER['REQUEST_URI']
答案 1 :(得分:1)
您可以尝试将第42行更改为:
header("Location: $_SERVER[PHP_SELF]",TRUE,302);
在解析html之前,重定向到新页面并使用sendcookies。
我建议使用ob_start并结束,但是:
ob_start();
//your html
$content = ob_get_contents();
ob_end_clean();