相信很多人在做网站的时候都需要用到此功能:浏览历史
即要记录用户在网站上的行为,很多人都明白要用cookie来实现,但具体怎么做会比较合理,并且代码比较精简呢?这的确会难到不少人的,为此,我把ecshop那段精简的代码贴出来,相信不少人真的会用到的:
/* 记录浏览历史 */
if (!empty($_COOKIE['ECS']['history']))
{
$history = explode(’,', $_COOKIE['ECS']['history']);
array_unshift($history, $goods_id);
$history = array_unique($history);
while (count($history) > $_CFG['history_number'])
{
array_pop($history);
}
setcookie(’ECS[history]‘, implode(’,', $history), gmtime() + 3600 * 24 * 30);
}
else
{
setcookie(’ECS[history]‘, $goods_id, gmtime() + 3600 * 24 * 30);
}
细细看上去,这真的很不错,希望对你有用

Del.icio.us
收藏到QQ书签
添加到雅虎收藏
Google书签
Baidu搜藏

