记住我不是在PHP中工作

时间:2016-04-12 08:04:50

标签: php

当我尝试使用选中的用户名和密码登录并记住我时,当我登录并再次返回登录页面时,我没有看到用户名和密码,为什么我这样做是什么?我做错了???

<?php
$title   = "Login Page";
$heading = "Login Form";
include "includes/home_page_header.php";
?>
<?php
$cookie_username = $cookie_password = "";


if(isset($_POST['login']))
{
    //echo "<pre>"; print_r($_POST); echo "</pre>";die;
    if(!empty($_POST['cookie_username']) && !empty($_POST['cookie_password']))
    {
        $cookie_username = $_POST["cookie_username"]; 
        $cookie_password = $_POST["cookie_password"];
$sql_users="SELECT * FROM users WHERE username='$username' AND password='$password'";
$res_users = mysqli_query($link,$sql_users);

    if(mysqli_num_rows($res_users) > 0 )
       {      
              $_SESSION["Username"]= $cookie_username;
              $_SESSION["Password"]= $cookie_password; 
              header('Location:http://localhost/sample/home_page.php');         
       }
       else
       {
            echo 'The username or password are incorrect!';
       }
    }


if($_POST['rememberMe'] =='on')
{       
    setcookie('cookie_username', $cookie_username, time() + (86400 * 30));
    setcookie('cookie_password', $cookie_password, time() + (86400 * 30));
    if(!isset($_COOKIE['cookie_username']))
    {
        echo "Cookie named '" . $cookie_username . "' is not set!";
    } 
    else 
    {
        echo "Cookie '" . $cookie_username . "' is set!<br>";
        echo "Value is: " . $_COOKIE['cookie_username'];
    }        
}
}
?>

<table class="login_table">
<form name="login" action="" method="post">
<tr>
<td>User Name :-</td>
<td><input type="text" name="cookie_username" value="<?php echo $cookie_username;?>"/></td>
</tr>
<tr>
<td>Password :-</td>
<td><input type="password" name="cookie_password" value="<?php echo $cookie_password;?>"/></td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name="remember" <?php if(isset($_COOKIE['remember'])) {
        echo 'checked="checked"';
    }
    else {
        echo '';
    }
    ?> >Remember Me
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="login" value="Login"/>
<input type="submit" name="cancel" value="Cancel"/></td>
</tr>
</form>
</table>

2 个答案:

答案 0 :(得分:2)

请勿将敏感数据存储在Cookie中

记住我的功能应该包含每次有人登录时生成的令牌,然后可以与数据库进行比较。如果用户希望在表单中记住他们的用户名/密码,则应通过单击&#34;记住我的登录详细信息&#34;来完成客户端站点。当他们登录时。

无论如何,要回答你的问题..

加载页面后,您将用户名/密码变量设置为空

$cookie_username = $cookie_password = "";

您应该将此行更改为

$cookie_username = isset($_COOKIE['cookie_username']) ? $_COOKIE['cookie_username'] : '';
$cookie_password = isset($_COOKIE['cookie_password']) ? $_COOKIE['cookie_password'] : '';

另外,请将您的复选框更改为

<input type="checkbox" name="rememberMe" value="on" <?=isset($_COOKIE['rememberMe']) ? 'checked="checked"' : ''?>

答案 1 :(得分:1)

您正在收集名为&#39; rememberMe&#39;的输入框。但是,您没有发布名为“记住我”的输入

if($_POST['rememberMe'] =='on')

您正在发布&#39;记得&#39;并收集&#39; rememberMe&#39;

<td><input type="checkbox" name="remember"

所以要么改变:

<td><input type="checkbox" name="remember"

<td><input type="checkbox" name="rememberMe"

或者

 if($_POST['rememberMe'] =='on')

if($_POST['remember'] =='on')

另外,您可能不应该在cookie中存储敏感信息;给THIS一个阅读。