我在Javascript中从readcookie函数获取非null响应时遇到问题。理想情况下,我会得到一个布尔响应,关于我的数据库中是否存在id,但是目前它只是出现null。我已经查看了存储的cookie,并且根本没有存储IDFound cookie。
Javascript方面:
function readCookie(name) {
$.get("/test/phpServerGet.php","function(result){}")
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0){ console.log("found")
return c.substring(nameEQ.length,c.length)};
}
return null;
}
PHP:
<?php
require_once 'path_to_db_connect.php';
$id_exists = false;
$CollegeID = $_COOKIE["CollegeID"];
echo $CollegeID;
$request = $connect->prepare("SELECT `CollegeID` FROM `users` WHERE `CollegeID` = ?;");
$request->execute(array($CollegeID));
$result = $request->fetch(PDO::FETCH_ASSOC);
$cookie_name = "IDFound";
if ($result['CollegeID'] == $CollegeID)
{ echo "YES";
$id_exists = true;
}
else if ($result['CollegeID'] != $CollegeID)
{
$id_exists = false;
}
setcookie($cookie_name, $id_exists, 1000);
?>
答案 0 :(得分:0)
setcookie($cookie_name, $id_exists, 1000);
3参数设置为1000,尝试用
替换它time() + 60
setcookie($cookie_name, $id_exists, time() + 60);
这应该让浏览器有更多时间来查找cookie并解析它。 time()+ 60将是一分钟。