1

当我勾选复选框(cookie)并按登录时,它没有进入会员页面。它标头到索引页面(当前页面)。

但是当我勾选复选框(cookie)并再次按日志时,它的标题是会员页面

任何人都可以帮助我问题出在哪里?

<?PHP
session_start();

function loggedin(){
    if(isset($_SESSION['log_email']) || isset($_COOKIE['log_email'])){
    $loggedin=TRUE;
    return $loggedin;   
    }
}

$log_email="";

if(loggedin()){
header("Location:member.php");}

if(isset($_POST['login'])){

    $log_email=strtolower($_POST['log_email']);
    $log_password=$_POST['log_password'];

    if(isset($_POST['cookie'])){
    $cookie=$_POST['cookie'];
    }


    if($log_email && $log_password){
        $connect=mysql_connect("localhost", "root", "");
        $database=mysql_select_db("phplogin", $connect);

        $SQL=mysql_query("SELECT * FROM users WHERE email='$log_email'");
        $numrows=mysql_num_rows($SQL);

        if($numrows!=0){
            while($result=mysql_fetch_assoc($SQL)){
                $db_email=$result['email'];
                $db_password=$result['password'];
                $db_firstname=$result['firstname'];
                $db_lastname=$result['lastname'];
            }

            if($log_email==$db_email && md5($log_password)==$db_password){      
                    if($cookie){
                        setcookie("log_email", $log_email, time()+7200);
                    }
                    else{
                    $_SESSION['log_email']=$log_email;
                    header("location:member.php");
                    exit();
                    }
            }
            else{echo"wrong username or password";}
        }
        else{echo "Can't find the user";}
    }
    else{echo "Please enter email or password";}
}


?>




<html>
<body>
<div id="container">
<div id="logo"></div>
    <div id="search"></div>
    <div id="top_search"></div>


    <div id="login">
        <form action="index.php" method="POST">
        <table>
            <tr>
                <td><lable id="td_1">Email</lable></td>
                <td><lable id="td_1">Password</lable></td>
            </tr>
            <tr>
                <td><input type="text" name="log_email" value="<?PHP echo $log_email; ?>" maxlength="50"/></td>
                <td><input type="password" name="log_password" maxlength="25" /></td>
                <td><input id="log_btn" type="submit" name="login" value="" /></td>
            </tr>
              <tr>
                <td><div id="td_2"><input type="checkbox" name="cookie"/>Remember me</div></td>
                <td><div id=td_2><a href="forgot_password.php">Forgot your password?</div></a></td>
            </tr>
        </table>
        </form>
    </div>   

</div>
</body>
</html>
4

1 回答 1

1

在打开此页面<html>之前,您会使用 以及更多数据<?php。仅当浏览器还没有输出时,标头才会起作用,因此这将不起作用(除非在您显示的代码之前页面上有某种输出缓冲处于活动状态。)

只需从 php 代码开始<?php并将其<html>和其他标签(包括换行符)移动到之后以检查登录。

然后不是直接输出错误消息,而是echo将它们存储在 var 中$error = "wrong username or password";,然后稍后在 HTML 代码中使用<?php echo $error; ?>

    <?PHP
    session_start();

    function loggedin(){
        if(isset($_SESSION['log_email']) || isset($_COOKIE['log_email'])){
        $loggedin=TRUE;
        return $loggedin;   
        }
    }

    $log_email="";
    $error = "";

    if(loggedin()){
    header("Location:member.php");}

    if(isset($_POST['login'])){

        $log_email=strtolower($_POST['log_email']);
        $log_password=$_POST['log_password'];

        if(isset($_POST['cookie'])){
        $cookie=$_POST['cookie'];
        }


        if($log_email && $log_password){
            $connect=mysql_connect("localhost", "root", "");
            $database=mysql_select_db("phplogin", $connect);

            $SQL=mysql_query("SELECT * FROM users WHERE email='$log_email'");
            $numrows=mysql_num_rows($SQL);

            if($numrows!=0){
                while($result=mysql_fetch_assoc($SQL)){
                    $db_email=$result['email'];
                    $db_password=$result['password'];
                    $db_firstname=$result['firstname'];
                    $db_lastname=$result['lastname'];
                }

                if($log_email==$db_email && md5($log_password)==$db_password){      
                        if($cookie){
                            setcookie("log_email", $log_email, time()+7200);
                            header("location:member.php");
                        }
                        else{
                        $_SESSION['log_email']=$log_email;
                        header("location:member.php");
                        exit();
                        }
                }
                else{$error = "wrong username or password";}
            }
            else{$error = "Can't find the user";}
        }
        else{$error = "Please enter email or password";}
    }


    ?>


    <html>
    <link href="CSS/login.css" rel="stylesheet" type="text/css" />

    <body>
    <div id="container">
    <div id="logo"></div>
        <div id="search"></div>
        <div id="top_search"></div>


        <div id="login">



            <?php
            if (strlen($error)>0)
              echo $error;
            ?>


            <form action="index.php" method="POST">
            <table>
                <tr>
                    <td><lable id="td_1">Email</lable></td>
                    <td><lable id="td_1">Password</lable></td>
                </tr>
                <tr>
                    <td><input type="text" name="log_email" value="<?PHP echo $log_email; ?>" maxlength="50"/></td>
                    <td><input type="password" name="log_password" maxlength="25" /></td>
                    <td><input id="log_btn" type="submit" name="login" value="" /></td>
                </tr>
                  <tr>
                    <td><div id="td_2"><input type="checkbox" name="cookie"/>Remember me</div></td>
                    <td><div id=td_2><a href="forgot_password.php">Forgot your password?</div></a></td>
                </tr>
            </table>
            </form>
        </div>   

    </div>
    </body>
    </html>
于 2012-12-06T13:21:20.140 回答