【发布时间】:2013-05-09 15:42:28
【问题描述】:
我正在使用 PHP,并希望实现以下目标(我知道这可以通过第三方插件实现,但我想自己构建它作为练习):
- 将用户在我的(Joomla 驱动的)网站中访问的所有 URL 的历史记录存储在 cookie 中。
- 当用户注销时,将一组值(包括 URL 历史记录在内的其他用户信息)发送到源(文件或数据库)。我还没有解决第 2 项,但如果能提供答案或好的指点将不胜感激。
到目前为止我创建的 PHP 代码:
$user = JFactory::getUser();
$helper = JUserHelper::getUserGroups($user->id);
if(!isset($_COOKIE['pagehistory'])){
setcookie('pagehistory',$_SERVER['REQUEST_URI'].'|');
}
else {
$_COOKIE['pagehistory'] .= $_SERVER['REQUEST_URI'].'|';
}
// debug: destroy cookie
//setcookie ("pagehistory", "", time() - 3600);
$group = "";
foreach ($helper as $value) {
$group .= $value."|";
}
$userinfo = array(
'id' => $user->id,
'username' => $user->username,
'realname' => $user->name,
'group' => $group,
'url' => $_SERVER['REQUEST_URI'],
'history' => $_COOKIE['pagehistory'],
);
我遇到的问题是“pagehistory”cookie。当我使用控制台进行测试时,我似乎只获得了第一个 URL 和永远压倒一切的第二个,但没有更多。
例子:
导航到 URL 1:'/' //(根)
导航到 URL 2:'/news'
导航到 URL 3:'/tutorials'
cookie 代码结果:
第一轮:'/'
第 2 轮:'/|/news' // '|'作为分隔符
第 3 轮:'/|/tutorials' // 代替 '/|/news|/tutorials'
我做错了什么?
【问题讨论】:
标签: php joomla2.5 httpcookie