无法修改标头信息 - 已发送的标头

时间:2016-08-14 08:37:29

标签: php

所以我有一个基本的登录系统。我试图在成功登录后使用标题重定向。但是我收到一条错误,上面写着"无法修改标题"我根本不熟悉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 (!@mysql_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文件,其中包含了#34;&#34;在索引页面的顶部。

<?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>

2 个答案:

答案 0 :(得分:0)

必须在页面中的任何内容之前发送PHP标头。从PHP文档:

  

HTTP状态标题行将始终是第一个发送到的   客户端,无论实际的header()调用是否是第一个。   可以通过调用具有新状态的header()来覆盖状态   除非已经发送了HTTP标头,否则在任何时候都行。

在您的文件中,您在设置标头之前发送了HTML链接标记,从而导致此错误。只需在任何输出之前放置PHP,它就可以正常工作

答案 1 :(得分:-1)

将此行放在php标记开头的顶部。

ob_start();

在这里你可以:

<?php
ob_start();
include('SqlConnect.php');
?>