我面临一个奇怪的问题。我将 AJAX 发送到一个 PHP 文件,该文件设置了 a$_COOKIE['cookieName']
然后我在主文件中回显该 cookie。
问题:如果处理 AJAX 的 PHP 文件与视图文件位于同一文件夹中,$_COOKIE['cookieName']
则会很好地回显。但是,如果我将它移动到不同的目录,Ajax 响应将成功通过,但 '$_COOKIE' 不会在视图文件中回显,就好像它从未设置过或不存在一样。
处理 AJAX 的文件:
$exp = time()+ 3600;
setcookie("cookieName", "tiger", $exp);
if(isset($_COOKIE['cookieName'])) {
echo "Ajax Response: " .$_COOKIE["cookieName"]. " cookie is set";
} else if(!isset($_COOKIE['cookieName'])) {
echo "Ajax Response: Session NOT SET";
}
视图文件:
<script>
$(document).ready(function(){
var boxText = "test";
$.ajax({
type: "POST",
url: "login.php",
//login.php is in the same directory, so $_COOKIE will echo below.
// If I moved the file to folder/login.php AJAX will come back successfully, but $_COOKIE won't echo...
data: {sendValue: boxText, ajaxSent: true},
success: function(response){
console.log(response);
}
});
});
</script>
<div >
Cookie name is.....<?php echo $_COOKIE['cookieName'];?>
</div>