【发布时间】:2014-10-15 18:37:39
【问题描述】:
我用谷歌搜索了 1-2 个小时,关于在 PHP 中将上次访问的页面存储在 cookie 中。但是一般都有JS的例子。你能推荐一个关于它的例子吗?或者你能举个例子吗?
我是 PHP 新手,我想做一些例子。
这个例子不起作用
// define the new value to add to the cookie
$ad_name = $myProductId; //comes from $_GET[]
// if the cookie exists, read it and unserialize it. If not, create a blank array
if(array_key_exists('recentviews', $_COOKIE)) {
$cookie = $_COOKIE['recentviews'];
$cookie = unserialize($cookie);
} else {
$cookie = array();
}
// add the value to the array and serialize
$cookie[] = $ad_name;
$cookie = serialize($cookie);
// save the cookie
setcookie('recentviews', $cookie, time()+3600);
//prints to screen noting
foreach ($_COOKIE['recentviews'] as $h) {
echo $h."-";
}
【问题讨论】:
-
什么鬼?为什么-1
-
在输出任何 html 内容(或空白)之前,您是否进行了所有这些 cookie 设置?
-
您也可以使用/调试:
$result = setcookie(...);结果是真/假,取决于它是否正确执行。
标签: php cookies session-cookies setcookie