无法修改标题信息 - 标题已发送
问题描述:
所以我有一个基本的登录系统。我试图在成功登录后使用标题重定向。但我得到一个错误,打印出“无法修改标题”我完全不熟悉PHP。但所有的帮助将不胜感激。无法修改标题信息 - 标题已发送
错误消息
Warning: Cannot modify header information - headers already sent by (output started at /home4/jachun39/public_html/ap/login.php:6) in /home4/jachun39/public_html/ap/login.php on line 22
的login.php文件
<?php
include('SqlConnect.php');
?>
<?php
if (isset($_POST['Login'])){
if ([email protected]_connect($host, $username, $password)) die("Can't connect to database");
if (!mysql_select_db($db_name)) die("Can't select database");
$username=$_POST['username'];
$password=$_POST['password'];
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count >= 1){
$_SESSION['username']= "username";
$_SESSION['password']= "password";
header("location: index.php?message=success");
echo "<center><font color='green'><b>Logged in Successfully</center></font></b>";
} else {
echo "<center><font color='red'><b>Wrong username or password</center></font></b>";
}
}
<link rel="Stylesheet" type="text/css" href="style.css" />
<link href='http://fonts.googleapis.com/css?family=Karla:400,700,700italic,400italic' rel='stylesheet' type='text/css'>
这里也是我Checklogin.php文件,该文件是 “包含” 在网站的首页顶部。
<?php
if(!isset($_SESSION['username'])){
header("location: login.php");
}
?>
///添加请求的(的index.php)
<?php
ob_start();
include('CheckLogin.php');
include('SqlConnect.php');
include('Userbar.php');
?>
<link rel="Stylesheet" type="text/css" href="style.css" />
<link href='http://fonts.googleapis.com/css?family=Karla:400,700,700italic,400italic' rel='stylesheet' type='text/css'>
</html>
<body>
<br>
<form name="form" method="POST" action=""><td>
<table width="325" border="0" align="center" cellpadding="2" cellspacing="0" bgcolor="#212121">
<td><table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#404040"></td>
<tr colspan="3"><strong> <font color="ECECEC"> Create Code </font></strong></tr>
<tr>
<td><center><font color="ECECEC">Code: <input name="code" type="text"></font>
<input type="Submit" value="Create Code" name="Addcode" /></td></tr></center>
</table></table>
</table></table>
</form>
<?php
if (!mysql_connect($host, $username, $password)){
die("Can't connect to database");
}
if (!mysql_select_db($db_name)){
die("Can't select database");
}
if (isset($_POST['Addcode'])){
mysql_query("INSERT INTO $table (username, password, hwid, ip, code, banned) VALUES('-','-','-','-','$_POST[code]','0')");
}
if (isset($_POST['Action'])){
if ($_POST['Action'] == "Delete"){
$ID = $_POST['ID'];
$result = mysql_query("DELETE FROM $table WHERE `id` = $ID");
if (!$result){
die(mysql_error());
}
} elseif ($_POST['Action'] == "Ban"){
$ID = $_POST['ID'];
$Banned = $_POST['Banned'];
if ($Banned == 0){
$result = mysql_query("UPDATE $table SET `banned` = '1' WHERE `id` = '".$ID."'");
} else {
$result = mysql_query("UPDATE $table SET `banned` = '0' WHERE `id` = '".$ID."'");
}
if (!$result){
die(mysql_error());
}
}
}
echo "<div class='table' align='center'><table><tr class='top'>";
echo "<td \"Username\"'>Username<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>";
echo "<td \"Password\"'>Password<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>";
echo "<td \"Hwid\"'>Hwid<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>";
echo "<td \"Code\"'>Code<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>";
echo "<td \"IP\"'>IP<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>";
echo "<td \"Active\"'>Active<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>";
echo "<td \"Delete\"'>Delete<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>";
echo "</tr>\n";
$type = "first";
$query = mysql_query("SELECT * FROM $table");
while($row = mysql_fetch_array($query)){
$ID = $row['id'];
$Username = $row['username'];
$Password = $row['password'];
$Hwid = $row['hwid'];
$Code = $row['code'];
$IP = $row['ip'];
$Banned = $row['banned'];
$Delete = "0";
echo '<td>'.$Username.'</td><td>'.$Password.'</td><td>'.$Hwid.'</td><td>'.$Code.'</td><td>'
.$IP.'</td><td>';
?>
<form action="" class="form" method="POST">
<input type="hidden" name="Action" value="Ban" />
<input type="hidden" name="ID" value=<?php echo "$ID";?> />
<input type="hidden" name="Banned" value=<?php echo "$Banned";?> />
<?php
if ($Banned == 1){
echo '<input type="image" src="img/cross.png" name="Ban" />';
} else {
echo '<input type="image" src="img/tick.png" name="Ban" />';
}
?>
</td><td>
</form>
<form action="" class="form" method="POST">
<input type="hidden" name="Action" value="Delete" />
<input type="hidden" name="ID" value=<?php echo "$ID";?> />
<input type="image" src="img/delete.png" name="Delete" />
</form>
</td>
<?php
echo "</tr>\n";
}
?>
</body>
</html>
答
放在上面这条线在您php
标签开始。
ob_start();
喜欢这里,您可以:
<?php
ob_start();
include('SqlConnect.php');
?>
答
PHP头必须在页面的任何内容之前发送。从PHP文档:
的HTTP状态标题行永远是首先发送到客户端 ,而不管实际的报头()调用为所述第一或不是。 除非已经发送了HTTP标头,否则可以随时通过调用标头()以及新状态 行来覆盖状态。
在您的文件中,您在设置导致此错误的标头之前发送了HTML链接标记。简单地把你的PHP之前的任何输出,它会工作
当我这样做,它不显示成功登录的消息了,只是重新加载 – John
它重新加载,因为你的头(“位置:index.php”) ;'这行代码。 尝试评论这一行,并看到结果一次 –
是的,现在它显示的消息,但我如何使其重定向到index.php?因为它已成功登录 – John