我用谷歌搜索了1-2个小时,用PHP存储最后访问过的页面。但通常有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."-";
}
答案 0 :(得分:-1)
$_COOKIE['recentviews']
仍然是一个序列化的字符串,所以这不会在循环中工作。
工作代码:
// 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;
$cookieString = serialize($cookie); /* changed $cookie to $cookieString */
// save the cookie
setcookie('recentviews', $cookieString , time()+3600); /* insert $cookiestring */
//prints to screen noting
foreach ($cookie as $h) { /* changed $_COOKIE['recentviews'] to $cookie */
echo $h."-";
}
更改在/ * .. * / comments。
中解释