I'm playing with PHP and am looking to achieve the following (I know this is possible with third party plug-ins, but I want to build this on my own as practice):
- Store the history of all URLs the user visits within my (Joomla driven) site in a cookie.
- Send an array of values (other user information including the URL history) to a source (file or database) when the user logs out. I have not tackled item two yet, but an answer or a good pointer would be appreciated.
The PHP Code I've created so far:
$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'],
);
The issue I'm having is with the 'pagehistory' cookie. When I do a test using the console, I seem to get only the first URL and the ever-overriding second, but no more.
Example:
Nav to URL 1: '/' //(root)
Nav to URL 2: '/news'
Nav to URL 3: '/tutorials'
Results of cookie code:
Round 1 : '/'
Round 2 : '/|/news' // '|' being the delimiter
Round 3 : '/|/tutorials' // instead of '/|/news|/tutorials'
What am I doing wrong?