cookie复选框故障

时间:2012-12-06 12:58:39

标签: php

勾选复选框(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>

1 个答案:

答案 0 :(得分:1)

在使用<html>打开之前,您可以使用<?php和更多数据开始此页面。标题仅在浏览器没有输出时才起作用,所以这不会起作用(除非在你显示的代码之前页面上有某种输出缓冲活动。)

<?php开始,将<html>和其他标记(包括换行符)移到php代码后面,以检查登录信息。

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

打印在HTML代码中
    <?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>