所以我第一次尝试使用COOKIES失败了。我正在尝试完成REMEMBER ME功能。我在youtube上观看了几个视频,并尝试了Stack Overflow上的几种方法。一定是我......
在我的INDEX.PHP上,我有这个登录表单:
<form action="index.php" method="get" id="loginForm" name="loginForm">
<input type="text" id="username" name="username" placeholder="Username" />
<input type="password" id="password" name="password" placeholder="Password"/>
<input type="checkbox" id="rememberMe" name="rememberMe" /> Remember Me
<button type="button" id="loginSubmit" name="loginSubmit">Sign In</button>
</form>
在同一页上,靠近底部,我有一些JQUERY:
$('#loginSubmit').on('click', function()
{
var username = $('#username').val();
var password = $('#password').val();
var rememberMe = $('#rememberMe').prop('checked');
// some validation to make sure no field is blank
// then post to a PHP script
$.post('api/checkLogin.php', {username: username, password: password, rememberMe: rememberMe}, function(data)
{
if(data == 0)
{
alert('username and password do not match');
return false;
}
else
{
window.location.href = "main.php";
}
});
checkLogin.php脚本如下所示(尽可能缩短):
<?php
include("../include/database.php");
include("../include/sessions.php");
if($_POST['username'] == true || $_POST['password'] == true)
{
$username = $_POST['username'];
$password = $_POST['password'];
$rememberMe = $_POST['rememberMe'];
$select = "SELECT username, password FROM table WHERE username = '$username'";
$query = mysqli_query($dbc, $select);
$row = mysqli_fetch_array($query);
$dbusername = $row['username'];
$dbpassword = $row['password'];
if($dbpassword == $password)
{
$_SESSION['username'] = $username;
// here's where I'm trying to set the cookie
if($rememberMe == "True")
{
$cookie_username = $username;
setcookie('username', $cookie_username, time()+(86400*7));
}
// send json if username/password matched
$out = 1;
echo json_encode($out);
mysqli_free_result($query);
}
else
{
$out = 0;
echo json_encode($out);
mysqli_free_result($query);
}
}
?>
当我成功地被重定向到main.php时,我试图回应COOKIE无济于事:
<?php
echo "cookie name is " . $_COOKIE['username'];
?>
当我使用Chrome的检查器时,它只显示:
"cookie name is "
我做错了什么?