当我尝试使用选中的记住我选项的用户名和密码登录时,当我登录并再次返回登录页面时,我在那里看不到用户名和密码,为什么我做错了什么???
<?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>
请不要将敏感数据存储在cookie中
记住我的功能应该包含一个类似令牌的东西,每次有人登录时都会生成该令牌,然后可以将其与数据库进行比较。如果用户想在表单中记住他们的用户名/密码,这应该通过在登录时单击"记住我的登录详细信息"来完成。
无论如何,为了回答你的问题。。
当页面加载时,您将用户名/密码变量设置为空
$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"' : ''?>
您正在收集名为"rememberMe"的输入框,但没有发布名为"rememberMe"的输入
if($_POST['rememberMe'] =='on')
您正在发布"记住"并收集"记住我"
<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中;读一读。