Page 1 of 1
Creating a browsing history array
Posted: Tue Feb 28, 2006 4:41 am
by divingdave
Hi there,
I'm relatively new to PHP as I'm currently learning from scratch so apologies if I use any wrong terms. Hope someone can offer some advice please...
I would like to create a browsing history array that remembers the last 3 pages the user viewed. The array should store each page's title and its full address (for displaying a link). Whenever a new page is viewed, the array should update itself by adding the most recent page visited and drop the oldest one, so it only keeps track of the last 3 pages viewed.
The concept I am trying to achieve is:
1) Check for the existance of the pages visited array (e.g. $keepTrack)
2) If $keepTrack doesn't exist then create it with 3 values of 'No History Available'.
3) Else if $keepTrack does exist then read it into 3 separate variables ($temp1, $temp2, $temp3).
4) Capture current page name and address into temporary variable ($thisPage).
5) Recreate array, storing $thisPage, $temp1, $temp2 as the 3 new values. $temp3 is dropped as its the oldest page.
6) Every time a new page is visited this script runs (as an include file) and the browsing history is tracked via the $keepTrack array.
Some of you might think, well why not just use the browser's back button to do the same thing. That is true, but I'm trying to create a panel down the side of the site which says 'Pages Recently Viewed' and has the page titles neatly displayed so the user can click on them.
Is what I'm try to do achievable without using cookies, and without using strings in the address bar?
Thanks,
Dave
Posted: Tue Feb 28, 2006 5:46 am
by jayshields
$_SERVER['HTTP_REFERER'] is the only way I can think of.
As the manual states, it cannot always be trusted because of it's reliability. Using this method alone your page would only be able to track one page before the user went to your website, but it will be able to track every page visited while on your website. Also, this method would not get the page title, only the URL.
Maybe using JavaScript you could do as you suggested and track the last 3, regardless of whether they're on your website or not, but I don't know about that.
pagetracker.php
Code: Select all
$tracker['thispage'] = $_SERVER['PHP_SELF'];
$tracker['pagebeforelast'] = $tracker['lastpage'];
$tracker['lastpage'] = $_SERVER['HTTP_REFERER'];
Something like that would builld the $tracker array. Obviously the code needs to be worked on, they are the basics.
Posted: Tue Feb 28, 2006 6:17 am
by divingdave
Thanks for the suggestion jayshields. I think I may have found a solution after trying out various methods. The following code is working exactly as I want on my local machine. Does it look OK?
Code: Select all
<?php
session_start();
$temp0 = $_SESSION["$keepTrack"][0];
$temp1 = $_SESSION["$keepTrack"][1];
$temp2 = $_SESSION["$keepTrack"][2];
if (empty($temp0)) { $temp0 = "No History Available"; }
if (empty($temp1)) { $temp1 = "No History Available"; }
if (empty($temp2)) { $temp2 = "No History Available"; }
echo "Previous page was " . $temp0 . "<br>";
echo "2 pages ago was " . $temp1 . "<br>";
echo "3 pages ago was " . $temp2 . "<br>";
$addCurrent = $_SERVER["SCRIPT_NAME"];
$_SESSION["$keepTrack"]=array($addCurrent,$temp0,$temp1);
?>
Obviously the above code is for testing purposes so all it does is display the following on screen:
Previous page was /company/profiles.php
2 pages ago was /company/index.php
3 pages ago was /company/history.php
I can make use of existing variables to pass the page title to the script.
The problem I had was that I was starting the session in an include file half way down the page, but I've since read that you must start a session before sending any HTML to the browser.
Posted: Tue Feb 28, 2006 6:22 am
by jayshields
Oh, sorry. Do you only wanted your history to track what's on your website? Not previous websites aswell like I thought.
In that case, SCRIPT_NAME or PHP_SELF or __FILE__ or something like that in the usage you've done will be just fine.
Seems like a nice script.
Posted: Tue Feb 28, 2006 6:32 am
by divingdave
Yes, I wanted it to show users a quick history of the pages they've visited only on my site - kind of like Amazon do when you're browsing their site - it keeps track of the last few items you viewed.
The script above remembers the last 3 pages you visited - and updates itself each time you go to a new page. If you have only just arrived at the site and you have no page viewing history then it inserts a value of 'No History Available' for each empty slot until they get filled up with the actual page history values.
Now, all I need to do is check for duplicates - because refreshing the page causes duplicate values in the array. So if you refreshed the page 3 times, the 3 history values in the array would be the same!
Cheers,
Dave
Posted: Tue Feb 28, 2006 6:57 am
by CoderGoblin
I would be tempted to use a single array to hold all the history details. This would enable you to change the number of history items simply without creating new variables.
Useful commands include count, unset, array_unshift and in_array. When displaying the history you could also use a foreach. This means if no history exists nothing is shown. Having "meaningless" information IMO is distracting to a user. You may also want to check for paramters passed in and disallow submitted forms.
Code: Select all
<?php
$title="WHATEVER NAME YOU WANT FOR SPECIFIC FILE";
// This section could be in a function (with $title passed in) or as part of an include
$max_history=3;
if (!isset($_SESSION['history'])) {
$_SESSION['history']=array('title'=>'$title','file'=>$_SERVER["SCRIPT_NAME"]);
} elseif ($_SESSION['history'][0]['file']<>$_SERVER['SCRIPT_NAME']) {
// Note only check for duplicates with the last entered page. Not within all the array.
array_unshift($_SESSION['history']=array('title'=>$title,'file'=>$_SERVER['SCRIPT_NAME']);
}
if ($_SESSION['history']>$max_history) {
unset($_SESSION['history'][$max_history];
}
// To display
foreach ($_SESSION['history'] as $previous) {
echo("<a href=\"{$previous['file']}\">{$previous['title']}</a><br />\n");
}
?>
You notice I save both the path/file and a meaning name($title). This $title would need to be set differently per file. If you ever wanted to increase the number of items in the history simply change the $max_history variable. Hopefully the code makes some sense to you...
Just my 2 cents...
Posted: Tue Feb 28, 2006 7:34 am
by divingdave
Thanks CoderGoblin. That looks much better than mine and I see you've included a check for duplicates too, thanks a lot.
Just had a go at using it but I can't seem to get it run, as it fails at the point of inclusion. My error log shows:
PHP Parse error: parse error, unexpected ';', expecting ')' on line 11 - the array unshift line.
Any thoughts?
=== EDIT ===
Sorry, added missing ) on lines 11 and 14 and it runs. Will have a play around with it and let you know how I get on...
=== EDIT ===
Cheers,
Dave
Posted: Tue Feb 28, 2006 7:55 am
by divingdave
Hi CoderGoblin,
Hope you don't mind me asking about the code example you posted. When I run it I get an error:
Wrong parameter count for array_unshift() in tracker.php on line 11
And the code output by the script is as follows:
Code: Select all
<a href="C">C</a><br />
<a href="/">/</a><br />
The C is the first letter of my page title - but somehow the rest is being lost. The C will change when I moved between pages to whatever the first letter of the page title is (e.g. D in 'Director Profiles' or S in 'Services'.)
What's strange though is the C in the href isn't related to my filename - it seems to be using the title value for the href too.
Also, when I visit more than 3 pages (when $max_history=3) the script still only ever outputs two links - always like above - with the 2nd link always empty. Only the 1st link updates between pages (to show <a href="D">D</a><br /> for example).
Any suggestions would be much appreciated...
Cheers,
Dave
Posted: Tue Feb 28, 2006 8:13 am
by CoderGoblin
Sorry about that...
Code: Select all
array_unshift($_SESSION['history']=array('title'=>$title,'file'=>$_SERVER['SCRIPT_NAME']));
should be
Code: Select all
array_unshift($_SESSION['history'],array('title'=>$title,'file'=>$_SERVER['SCRIPT_NAME']));
also looking at the code again change
Code: Select all
unset($_SESSION['history'][$max_history]);
to
Joys of coding thoughts without any testing...
Posted: Tue Feb 28, 2006 8:52 am
by divingdave
Thanks for the update. Just tried it again. Sorry to be a pain but I now get the following in the error log:
[Tue Feb 28 14:47:44 2006] [error] PHP Warning: array_unshift() [<a href='function.array-unshift'>function.array-unshift</a>]: The first argument should be an array in /phpincludes/tracker.php on line 11
[Tue Feb 28 14:47:44 2006] [error] PHP Warning: Invalid argument supplied for foreach() in /phpincludes/tracker.php on line 18