-1

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

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");
    ?> 
4

7 回答 7

1

您的代码开头有一个空格..

<?php

删除后尝试。

有关更多信息,请查看

于 2012-10-22T05:11:32.107 回答
1

您的<?php标签前可能有一个空格。看起来它在您的示例中。

于 2012-10-22T05:12:09.230 回答
1

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

于 2012-10-22T05:12:52.707 回答
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");
于 2012-10-22T05:19:37.673 回答
0

制作没有结束标签的 php 文件是一个好习惯:

?>

只需写如下内容:

//end of file

例如,您可以在 codeIgniter php 文件中看到它

于 2012-10-22T05:13:56.747 回答
0

相同的问题/问题

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

于 2012-10-22T05:14:52.127 回答
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>
于 2012-10-22T05:17:20.177 回答