PHP标头已发送错误

时间:2012-10-22 05:08:56

标签: php mysql login header

我的代码遇到了问题。如果登录正常,我应该重定向到一个页面。我正在成功登录但是我收到了一个错误的标题正向。这是问题所在的行发生:

header("Location: members.php");

以下是错误消息: 警告:无法修改标头信息 - 已发送的标头

如果有帮助的话,这是我的完整代码:

<?php 
    // Connects to your Database 
    include("dbconnect.php");
    mysql_select_db("maxgee_close2");
    //Checks if there is a login cookie

    if(isset($_COOKIE['ID_my_site']))
     //if there is, it logs you in and directes you to the members page
    { 
        $username = $_COOKIE['ID_my_site']; 
        $password = $_COOKIE['Key_my_site'];
        $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
        while($info = mysql_fetch_array( $check )) 
        {
            if ($pass != $info['password'])
            {
            }
            else
            {
                header("Location: members.php");
            }
        }
    }
    //if the login form is submitted 
    if (isset($_POST['submit'])) { // if form has been submitted
     // makes sure they filled it in
        if(!$_POST['username'] | !$_POST['password']) {
            die('You did not fill in a required field.');
        }
        // checks it against the database

        if (!get_magic_quotes_gpc()) {
            $_POST['email'] = addslashes($_POST['email']);
        }
        $check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());

        //Gives error if user dosen't exist
        $check2 = mysql_num_rows($check);
        if ($check2 == 0) {
            die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');
        }
        while($info = mysql_fetch_array( $check ))  
        {
            $_POST['password'] = stripslashes($_POST['password']);

            $info['password'] = stripslashes($info['password']);

            $_POST['password'] = md5($_POST['password']);

            //gives error if the password is wrong

            if ($_POST['password'] != $info['password']) {
                die('Incorrect password, please try again.');
            }
            else 
            { 
                // if login is ok then we add a cookie 
                $_POST['username'] = stripslashes($_POST['username']); 
                $hour = time() + 3600; 

              //then redirect them to the members area and the line with the error
              header("Location: members.php");
            }
        } 
      }
      else
      { 
        // if they are not logged in
         ?>
         <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
         <h1>Login</h1>
         Username:
        <input type="text" name="username" maxlength="40"> 
        Password:
        <input type="password" name="password" maxlength="50"> 
        <input type="submit" name="submit" value="Login">
        </form> 
    <?php 
     } 
     include("topsite.php");
    ?> 

7 个答案:

答案 0 :(得分:1)

代码开头有一个空格..

<?php

删除后再尝试。

有关详细信息,请查看this

答案 1 :(得分:1)

您的<?php代码前可能有空格。看起来它就像你的例子一样。

答案 2 :(得分:1)

在php标签之前有一个空格。 PHP标记之外的任何空格都将被视为输出空白。

答案 3 :(得分:1)

你写过

$password = $_COOKIE['Key_my_site'];

但是你将$ pass与$ info ['password']进行比较,它应该是

    if ($password != $info['password']){

    } else {

        header("Location: members.php");
    }

您可以通过编写

以更好的方式修改查询
$check = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password'")or die(mysql_error());

if(mysql_num_rows($check) == 0) header("Location: members.php");

答案 4 :(得分:0)

这是一个很好的做法,可以制作没有结束标记的php文件:

?>

只需写下:

//end of file

你可以在codeIgniter php文件中看到它

答案 5 :(得分:0)

same question/problem

尝试在开始时添加ob_start(),在结束时添加ob_flush()ob_end_flush()

答案 6 :(得分:0)

尝试使用

<?php ob_start();?> // top of the page

<?php ob_flush();?> // at the end of the page

这两个用于启动输出缓冲区。如果我们使用这个,那么输出到用户只会在该文档中每行的清除之后抛出

并尝试删除 session_start()之前的空格。

eg: <?php session_start();?>
    <!DOCTYPE html>
    <html>
相关问题