有人可以在测试机器上运行此代码吗?我有点困惑,为什么 cookie 可以“取消设置”但不能在 php 中更改值或过期等,似乎 setcookie() 根本不起作用。
<script type="text/javascript" src="jquery.js"></script>
<script src="jquery.cookie.js"></script>
<script>
$.cookie('myCookie', '123' ,'/');
console.log("Created myCookie");
</script>
<?php
echo $_COOKIE['myCookie'];
//Comment/uncomment below as required
//setcookie("myCookie", "BLAH", time()-430000);
//$_COOKIE['myCookie'] = "BLAH";
setcookie('myCookie', '', 1, '/');
echo "<br />unset myCookie";
echo "<br />".$_COOKIE['myCookie'];
?>
<script>
console.log($.cookie('myCookie').length);
if ($.cookie('myCookie').length>0)
{
console.log('Cookie exists ');
console.log($.cookie('myCookie'));
}
else
{
console.log('Yaay! No cookie!');
}
</script>
您似乎能够创建一个 php <> JS cookie mismatch 即:2 个 cookie 调用完全相同但存储不同的数据。