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

时间:2011-05-30 11:42:45

标签: php login header warnings whitespace

  

可能重复:
  “Warning: Cannot modify header information - headers already sent by” error

大家好,

我尝试登录时会看到一条警告消息。你能帮我纠正这个问题。

Warning: Cannot modify header information - headers already sent by (output started at /path/to/login.php:15) in /path/to/login.php on line 231

这是html之前的php代码:

<?php
        //allow sessions to be passed so we can see if the user is logged in
        session_start();
        //connect to the database so we can check, edit, or insert data to our users table
        $con = mysql_connect('XXXXXXXXXXXXXXX', 'XXXXXXXXXXXX', 'XXXXXXXXXX') or die(mysql_error());
        $db = mysql_select_db('XXXXXXXXXXXXXXXXX', $con) or die(mysql_error());
        //include out functions file giving us access to the protect() function made earlier
        include "functions.php";
?>

这是functions.php文件:

<?php

function protect($string){
    $string = trim(strip_tags(addslashes($string)));
    return $string;
}

?>

最后这里是HTML中的PHP:

<?php

        //If the user has submitted the form
        if($_POST['submit']){
            //protect the posted value then store them to variables
            $username = protect($_POST['username']);
            $password = protect($_POST['password']);

            //Check if the username or password boxes were not filled in
            if(!$username || !$password){
                //if not display an error message
                echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>";
            }else{
                //if the were continue checking

                //select all rows from the table where the username matches the one entered by the user
                $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'");
                $num = mysql_num_rows($res);

                //check if there was not a match
                if($num == 0){
                    //if not display an error message
                    echo "<center>The <b>Username</b> you supplied does not exist!</center>";
                }else{
                    //if there was a match continue checking

                    //select all rows where the username and password match the ones submitted by the user
                    $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");
                    $num = mysql_num_rows($res);

                    //check if there was not a match
                    if($num == 0){
                        //if not display error message
                        echo "<center>The <b>Password</b> you supplied does not match the one for that username!</center>";
                    }else{
                        //if there was continue checking

                        //split all fields fom the correct row into an associative array
                        $row = mysql_fetch_assoc($res);

                        //check to see if the user has not activated their account yet
                        if($row['active'] != 1){
                            //if not display error message
                            echo "<center>You have not yet <b>Activated</b> your account!</center>";
                        }else{
                            //if they have log them in

                            //set the login session storing there id - we use this to see if they are logged in or not
                            $_SESSION['uid'] = $row['id'];
                            //show message
                            echo "<center>You have successfully logged in!</center>";

                            //update the online field to 50 seconds into the future
                            $time = date('U')+50;
                            mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");

                            //redirect them to the usersonline page
                            header('Location: usersOnline.php');
                        }
                    }
                }
            }
        }   

?>

当我尝试将它们重定向到我的usersOnline.php页面时,会出现主要问题。

//redirect them to the usersonline page
                            header('Location: usersOnline.php');

我已将所有空白区域最小化,但仍有问题。错误告诉我的地方,第15行是我的元ID(描述和关键字 - 搜索引擎优化)。这会在PHP中造成问题吗?

我非常感谢任何帮助,因为我对PHP相对较新。

谢谢, 达伦

4 个答案:

答案 0 :(得分:4)

您打印“您已成功登录!”在发送Location标头之前,这是不允许的。如果要重定向,则不应生成任何输出。

答案 1 :(得分:3)

尝试放入ob_start();在login.php的第一行。这将缓冲输出,即使您已经向浏览器发送了一些文本,也可以进行重定向。

答案 2 :(得分:1)

正如@adam所说,你无法向屏幕回显任何内容并使用header命令重定向。

不是显示您的消息,而是将消息保存为变量(例如$ message)并将重定向替换为:

$display = "
<HTML>
<HEAD>
<META HTTP-EQUIV="refresh" CONTENT="seconds;URL=the-other-url">
</HEAD>
<BODY>
".$message."
</BODY>
</HTML>
echo ($display);

秒可以替换为5以显示您的消息5秒,并且“the-other-url”应替换为usersOnline.php。

答案 3 :(得分:0)

你为什么要这样做呢?用户甚至无法看到该消息,因为他们被重定向到另一个页面后立刻就会看到。

不推荐使用<center>代码:

  

CENTER元素完全等同于指定DIV   具有P属性集的元素(或在这种情况下更好的align元素)   到&#34;中心&#34;。 CENTER元素是   弃用。   http://www.w3.org/TR/html4/present/graphics.html#edef-CENTER

修改

在此期间,align似乎也被弃用了。请改为使用<p>标记的CSS类。 http://www.w3.org/TR/1999/REC-html401-19991224/present/graphics.html#adef-align