0

我正在学习如何使用“setcookie”,但是,我无法使用以下线路,我已将所有代码粘贴在这里,如果有人可以帮帮我吗?不知道原因。非常感谢。

  else{ die ("hahahahahahahahahahahahahaha"); }

HTML 代码

<form method="POST" action="">
<div class="error"><?php echo $error;?></div>
<p></p>
<label>Username: </label><br>
<input type="text" name="username"><br>
<label>Password: </label><br>
<input type="password" name="password"><br>
<input type="checkbox" name="rememberme"> Remember me<br>
<input type="submit" name="submit" value="Login">

PHP代码

<?
if(isset($_POST['submit'])){
    //get data
    $username = $_POST['username'];
    $password = $_POST['password'];
    $rememberme = isset($_POST['rememberme']);
    echo $rememberme;


    if($username&&$password){
    $login = mysql_query("SELECT * FROM form WHERE username='$username'");

    while ($row = mysql_fetch_assoc($login))
    {
        $db_password = $row['password'];
        if (md5($password)== $db_password)
        {
            $logstatus = TRUE;
            }
        else{
            $logstatus = FALSE; 
            }
        if ($logstatus == TRUE)
        {
            if ($rememberme == "1")
                setcookie("username", $username, time()+600);
            else if ($rememberme == "")
                $_SESSION['username'] = $username;


        echo " I am here";
        }

        else{
           die ("hahahahahahahahahahahahahaha"); //unable to get here
        }
    }

}

    else{
    echo "enter username / password";
    }
}
?>
4

3 回答 3

0

试试这个代码,我没有测试过,但应该可以工作:)

session_start();//dont forget this :P
if(isset($_POST['submit'])){
    //get data
    $username = $_POST['username'];
    $password = $_POST['password'];
    $rememberme = isset($_POST['rememberme']);
    echo $rememberme;    
if($username&&$password){
    $login = mysql_query("SELECT * FROM form WHERE username='$username' AND password='".md5($password)."'");


        if (mysql_num_rows($login))//if this returns 1 you are logged in
        {
            if ($rememberme == "1")
                setcookie("username", $username, time()+600);
            else
                $_SESSION['username'] = $username;


                echo " I am here";              

        }else{
           die ("Incorrect Username/Password"); //unable to get here
        }
    }

}

    else{
    echo "enter username / password";
    }
}
于 2012-08-09T03:56:57.360 回答
-1

while 循环导致问题,只需将其删除即可。

于 2012-08-09T03:49:23.430 回答
-1

好吧,我没有测试代码,但尝试以下可能会有所帮助。

添加行:

$logstatus = TRUE;

前一阵子。

理由:变量的范围在块完成后立即完成。在 while 之外定义 logstatus 将确保其范围不会在需要的地方结束。

于 2012-08-09T03:52:50.703 回答