无法修改标题信息 - 已发送标题
问题描述:
亲爱的朋友们,我有奇怪的错误,我无法解决,需要你的帮助。代码工作正常,当登录不正确和登录正确时,它仍然会创建一个会话,但不会重定向到index.php,而是会抛出一个错误“无法修改标头信息 - 标头已被...发送”。我已经寻找类似的职位,但未能解决问题无法修改标题信息 - 已发送标题
这里是代码
<?php include ("scripts/connection.php");
session_start();
$myusername = $_POST["myusername"];
$myusername = preg_replace('/[^a-zA-Z0-9\']/','',$_POST['myusername']);
$mypassword = $_POST["mypassword"];
$mypassword = preg_replace('/[^a-zA-Z0-9\']/','',$_POST['mypassword']);
$LoginQuery = "SELECT * FROM users WHERE username='$myusername' and password='$mypassword'";
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Admin Login</title>
</head>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr><form name="form1" method="post">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<?php if($_SERVER["REQUEST_METHOD"] == "POST")
{ $result = $mysql->query($LoginQuery);
if($result->num_rows ==1)
{
$_SESSION['login_user']=$myusername;
header("Location:index.php");
exit();
}
else
{
echo '<tr><td width="78"> </td><td width="6"></td><td width="294"><font style="color:#f00; font-weight:bold;">Invalid!</font></td></tr>';
}
}
?>
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
朋友可以ü找到我做错了什么,并帮助我纠正它,请。 谢谢
答
你必须理解HTTP使用前功能header
:
一个redirct由HTTP标头实现。 在HTTP标头发送,之前任何输出(如网页本身)
所以你正试图发送有效载荷后发送重定向头。
这是不可能的。
总之,在打印出页面上的东西后,切勿使用header()。
答
你应该写其他任何文本
这个错误确实很奇怪。它已经回答了1000多次,尽管所有的答案都在右栏--- --->中,但人们依然在问 – 2013-02-17 14:07:55