我正在创建一个小商店并使用 cookie 来存储购买的商品。 当将商品添加到购物车时,我会执行以下操作:
document.cookie="itemname=added";
然后我有一个按钮可以转到购物车,在 php 中我可以这样做:
foreach($_COOKIE as $cookie=>$value)
{
if ($value=="added")
...something
}
一切正常,除了我需要重新加载页面才能将商品显示在购物车中。如果我向购物车链接添加 ?u=randomvalue
,它也可以工作,但由于某些原因,它非常烦人。
这是我的 html 标题:
<!DOCTYPE HTML>
<html lang="fr">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<link href="/style.css" rel="stylesheet" type="text/css">
<title>Shop</title>
<meta name="description" content="">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
</head>
有什么我忘记了吗?
谢谢
最佳答案
我必须添加这样的 php header :
<?
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-cache");
header("Cache-Control: post-check=0, pre-check=0");
header("Pragma: no-cache");
?>
关于javascript - 无缓存和 cookie 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42610122/