我正在尝试使用php实现cookie以便记住我的功能。但是,我不认为使用我的代码,cookie会被启用。
<?php require_once 'config.php'
?>
<!DOCTYPE html>
<html >
</head>
<body>
<form class="Login" method="post" action="private.php">
<div type="submit" class="col-md-6 col-xs-6 login">Login</div>
<div type="submit" class="col-md-6 col-xs-6 sign_up">Sign up</div>
<input type="text" class="col-md-10 col-xs-10 username" id="username_login" placeholder="abc@xyz.com" name="member_name" value="<?php if(isset($_COOKIE["member_login"])) { echo $_COOKIE["member_login"]; } ?>" />
<input type="password" class="col-md-10 col-xs-10 password" id="password_login" placeholder="Password" name="member_password" value="<?php if(isset($_COOKIE["member_password"])) { echo $_COOKIE["member_password"]; } ?>"/>
<div class="row remember col-md-11 col-xs-11">
<label><input type="checkbox" name="autologin" id="checkbox" name="remember" <?php if(isset($_COOKIE["member_login"])) { ?> checked <?php } ?>/> Remember Me </label>
</div>
<div class="row col-md-11 col-xs-11">
<button type="submit" id="button" name="login">Login</button>
</div>
</form>
</body>
</html>
我在其中设置cookie值的配置文件是这样的:
<?php require 'database.php';
error_reporting(E_ALL ^ E_NOTICE);
session_start();
if(!empty($_POST["login"])) {
#$conn = mysqli_connect("localhost", "root", "", "blog_samples");
$sql = "Select * from login where Username = '" . $_POST["member_name"] . "' and Password = '" . ($_POST["member_password"]) . "'";
$result = mysqli_query($conn,$sql);
$user = mysqli_fetch_array($result);
if($user) {
if($_POST["remember"]==1) {
setcookie ("member_login",$_POST["member_name"],time()+ (10 * 365 * 24 * 60 * 60));
setcookie ("member_password",$_POST["member_password"],time()+ (10 * 365 * 24 * 60 * 60));
setcookie($_POST["remember"],1);
header("location:private.php");
}
}
}
?>
结束页面,即private.php就像这样
<?php
echo"Hello";
session_start();
#$_SESSION["member_id"] = "";
session_destroy();
#header("Location:private.php");
?>
当我刷新浏览器时,使用用户名和密码登录并记住我检查后,输入字段不会预填充相同的用户名和密码。我认为cookie没有设置。这是正确的这样做的方式?