我已经完成了
<?php
echo "initial values in the cart: <pre>";print_r($_COOKIE);echo "</pre>";
$_COOKIE['cart']=null;
setcookie("cart", "", time()-3600);
unset($_COOKIE['cart']);
unset($_COOKIE);
?>
<script>
alert( "before delete " +$.cookie("cart") );
</script>
<script>
$.cookie("cart")="";
$.cookie('cart', null);
$.cookie('cart',null,{path:'/');
$.cookie("cart")=null;
$.cookie('cart', '', { expires: -150 });
jQuery.cookie("cart", '',{expires: -1,path:'/'});
$.cookie("cart")=undefined;
delete $.cookie("cart");
$.cookie("cart","",);
$.removeCookie('cart', { path: '/' });
$.removeCookie("cart");
alert( "just delete " +$.cookie("cart") );
</script>
<script>
if($.cookie('cart') === null || $.cookie('cart') === "")
{
alert( " cookie deleted 1 " +$.cookie("cart") );
}
else
{
//have cookie
alert( "have cookie " +$.cookie("cart") );
}
if (typeof $.cookie('cart') === 'undefined'){
//no cookie
alert( " cookie deleted 2 " +$.cookie("cart") );
}
// if($.cookie('cart') === null || $.cookie('cart') === ""
// || $.(cookie('cart') === "null" || $.cookie('cart') === undefined))
// {
// alert( " cart cookie deleted 3 " +$.cookie("cart") );
// }
</script>
<script>
alert( "after delete " + $.cookie("cart") );
</script>
<?php
echo "Items after cookie in the cart is unset: <pre>";print_r($_COOKIE);echo "</pre>";
?>
输出是:
(1)它在购物车中打印初始值,cookie有购物车变量设置
(2)删除警报之前
(3)有cookie警报
(4)删除警报后
(5)print_r
投掷通知Undefined variable: _COOKIE
见解:
- &GT;主要是通过jquery设置cookie,并且能够通过输出(1)来访问它
- &GT;接下来我unset
Cookie print_r
投掷说Undefined variable
并通过警报我仍然可以看到cookie值,我试图以jquery方式删除cookie但是在所有警报中cookie值都是显示,最后再次print_r($_COOKIE)
说Undefined variable
- &GT;我希望页面刷新输出(1),即print_($_COOKIE['cart'])
必须说Undefined variable cart
但仍然返回原始/旧cookie-cart变量。
请建议我如何正确设置cookie
使用mozilla的cookie附加组件,我发现cookie的路径是正确的,如上所述
的解决
把jquery放在一边我试了setcookie('cart', null, -1, '/');
它解决了我的问题!
谢谢!
答案 0 :(得分:1)
需要考虑的事项:
Example:
在php中设置cookie并在php中更改它,需要刷新。/
,将路径设置为domain.com
,这正是您需要发送以删除Cookie的路径。例如:设置Cookie:
setcookie("cart", "my content", time()+3600, "/", "domain.com");
取消设置Cookie:
setcookie("cart", "", time()-3600, "/", "domain.com");