Click to See Complete Forum and Search --> : Tracking and displaying users' movement on a website with PHP?
menre
Nov 17th, 2009, 07:10 AM
Hi All,
Maybe you have all noticed this on one website or the other before. Sometimes you visit a website and when you click on a page or two, it shows somewhere on top of the page how you have or the path that you have taken to get to that page. For example, it could be like
'Home > Students > Course Search > Undergraduate'
Which means, you first visited the home page, then visited Students, then Course Search, and then Undergraduate.
I am trying to do something like that for a website. Is it possible to do it with PHP? Could someone tell me how to start or do this please?
I will really appreciate your help.
Thanks,
Menre
kfcSmitty
Nov 17th, 2009, 07:37 AM
What you're looking for is called breadcrumbs. If you Google it you'll find a bunch of examples like below.
http://www.mindpalette.com/tutorials/breadcrumbs/index.php
menre
Nov 17th, 2009, 09:52 AM
Hello,
Thanks for the info and help. I didn't know what the name was before, but now I know. The link was useful.
Once again, thanks.
Nightwalker83
Nov 18th, 2009, 06:09 AM
Are you able to get that code to work correctly? I can't get it to show the name of the current page in the breadcrumb.
menre
Nov 18th, 2009, 06:37 AM
Hello,
There seems to be something out of place with the display. I tired them on two different servers. You are right. On one server, it shows just HOME, but not the current page. While on the other server, it shows HOME and the rootfolder as the current page, but also no the name of the current page.
There are two lines of code he suggested. I will try both out and get back to you.
Menre
Nightwalker83
Nov 18th, 2009, 04:39 PM
There are two lines of code he suggested. I will try both out and get back to you.
If you get the working could you please post it? I'm having a hard time trying to figure out how to get it to work too.
menre
Nov 19th, 2009, 07:30 AM
Hi,
There is still no breakthrough yet with this code as far as my work is concerned. I have tried both lines of code he gave and they offer the same thing. For example, if you have a folder called students and the folder has two files in it, say one called undergraduate.php and the second file called postgraduate.php and there are two links from your home page to those pages in the folder. When you visit your home page and then click on any of the links say undergraduate, the page opens and the breadcrumbs shows HOME>STUDENTS.
Instead of showing undergraduate, it actually show the rootfolder.
I have checked a few other websites using breadcrumbs and noticed that something is also not right with many of them. I cannot confirm if they did it in php or JavaScript.
For example, I visited the home page of a site, then another page Computing & IT, but the breadcrumbs showed homepage > Jobs > Computing & IT. But I did not visit the jobs page.
I visited another one and it showed something similar. From their home page I click on a link for track. But when the page opened I got automatically three pages for the breadcrumbs "Home > Students > Track" even though I had only just made one click. From the result of our code and the sites that I have visited so far, it seems to me that for the code to work fine, the files will have to be inside a sub folder.
For example, if you read the URL below carefully, you will see how subfolders have been added and each subfolder containing an index.php file.
http://www.mindpalette.com/tutorials/breadcrumbs/index.php
The above link produces the breadcrumbs below.
HOME > Tutorials > Breadcrumbs. Ideally, you would expect another page (index) to be in front of Breadcrumbs, but this is not the case.
I am still looking into it and will get back again once I have got it right.
Menre
kfcSmitty
Nov 19th, 2009, 07:37 AM
Wait, so you're looking to dynamically show which pages the user has viewed? I had simply thought you wanted to show the structure on how to get to the page you're viewing.
The only way I know to track the exact pages the user has previously viewed is to log it (perhaps in a database?) based on IP or a session or something. Breadcrumbs are used to show which directory you're in. So even if you went Home -> Programming, and prorgramming is under the IT folder, it will show Home -> IT -> Programming.
menre
Nov 19th, 2009, 10:22 AM
Hi,
Thanks for your response and your example. But based on your example, the problem we are having here now is, with this piece of code we are working with, programming will not display at all.
So even if you went Home -> Programming, and prorgramming is under the IT folder, it will show Home -> IT -> Programming.
That is understood now. However, instead of showing "Home>IT>Programming", it just would show "Home>IT" but no Programming even though you are on that programming page. I agree with your explanations and that is how the sites I went to really display theirs.
How to make the current page we are on display as well is what is missing at the moment. Any other advice will be appreciated.
Thanks,
Menre
kfcSmitty
Nov 19th, 2009, 11:13 AM
Judging by the quick look at the code, it seems you need an index.php in each directory you want displayed. If it does not contain an index.php, it probably will not display.
Scripts like these are based upon directories, and not the links. For it to display Home -> IT -> Programming you would need
Root Dir -> index.php
IT Dir -> index.php
Programming Dir -> index.php
SambaNeko
Nov 19th, 2009, 11:24 AM
Even with an index.whatever page in the directory, it looks like it still doesn't display the page you're on in the breadcrumb chain. Try this modification...
<?php
$convert_toSpace = true; // true if script should convert _ in folder names to spaces
$upperCaseWords = true; // true if script should convert lowercase to initial caps
$topLevelName = "HOME"; // name of home/root directory
$separator = " > "; // characters(s) to separate links in hierarchy (default is a > with 2 spaces on either side)
// find index page in directory...
function MPBCDirIndex($dir) {
$index = '';
@$dir_handle = opendir($dir);
if ($dir_handle) {
while ($file = readdir($dir_handle)) {
$test = substr(strtolower($file), 0, 6);
if ($test == 'index.') {
$index = $file;
break;
}
}
}
return $index;
}
// make clean array (trim entries and remove blanks)...
function MPBCTrimArray($array) {
$clean = array();
for ($n=0; $n<count($array); $n++) {
$entry = trim($array[$n]);
if ($entry != '') $clean[] = $entry;
}
return $clean;
}
// function to prep string folder names if needed...
function MPBCFixNames($string) {
global $convert_toSpace;
global $upperCaseWords;
if ($convert_toSpace) $string = str_replace('_', ' ', $string);
if ($upperCaseWords) $string = ucwords($string);
return $string;
}
$server = (isset($_SERVER)) ? $_SERVER : $HTTP_SERVER_VARS;
$htmlRoot = (isset($server['DOCUMENT_ROOT'])) ? $server['DOCUMENT_ROOT'] : '';
if ($htmlRoot == '') $htmlRoot = (isset($server['SITE_HTMLROOT'])) ? $server['SITE_HTMLROOT'] : '';
$pagePath = (isset($server['SCRIPT_FILENAME'])) ? $server['SCRIPT_FILENAME'] : '';
if ($pagePath == '') $pagePath = (isset($server['SCRIPT_FILENAME'])) ? $server['SCRIPT_FILENAME'] : '';
$httpPath = ($htmlRoot != '/') ? str_replace($htmlRoot, '', $pagePath) : $pathPath;
$dirArray = explode('/', $httpPath);
$dirArray[count($dirArray)-1] = substr($dirArray[count($dirArray)-1],0,strrpos($dirArray[count($dirArray)-1],"."));
if ($upperCaseWords) $dirArray[count($dirArray)-1] = ucwords($dirArray[count($dirArray)-1]);
$linkArray = array();
$thisDir = '';
$baseDir = ($htmlRoot == '') ? '' : $htmlRoot;
for ($n=0; $n<count($dirArray); $n++) {
$thisDir .= $dirArray[$n].'/';
$thisIndex = MPBCDirIndex($htmlRoot.$thisDir);
$thisText = ($n == 0) ? $topLevelName : MPBCFixNames($dirArray[$n]);
$thisLink = ($thisIndex != '') ? '<a href="'.$thisDir.$thisIndex.'">'.$thisText.'</a>' : $thisText;
if ($thisLink != '') $linkArray[] = $thisLink;
}
$results = (count($linkArray) > 0) ? implode($separator, $linkArray) : '';
if ($results != '') print('<div class="backlinks">'.$results.'</div>');
?>
...is that what you're looking for?
Nightwalker83
Nov 19th, 2009, 04:54 PM
@ SambaNeko,
That is an improvement on the originally code is there anyway to change or remove the php link from the breadcrumb? It points to the directory where the file is instead of the location above current page on the site.
menre
Nov 19th, 2009, 04:57 PM
Yep,
You did it. That is exactly what I am looking for. I have tested it and it is working. I am sure Nighthawker and everyone will find it useful.
Well done! Thanks a million for the code.
Menre
menre
Nov 20th, 2009, 06:52 PM
Hello,
Sorry that I have to reopen this topic. The problem is actually still there. Nighthawker was right and I accept I was wrong. I did not check my work properly before I started to celebrate and that was wrong of me.
The problem this time is that the code you modified now does the opposite. Before, the old code shows just HOME without the current page. But after the modification, the code now show the current page without HOME.
Please see how everything is.
Before: HOME>STUDENTS (no Undergraduate even when on that page)
Now: >Undergraduate (no HOME, no Students)
Could you have a look at the code again please? Thanks for your help.
Nightwalker83
Nov 20th, 2009, 08:02 PM
Could you have a look at the code again please? Thanks for your help.
For me the code shows:
Home > php > Index
Rather than say:
Home > Products > Shopping Cart
Nightwalker83
Nov 23rd, 2009, 07:05 AM
@ Samba Neko,
How could I modify the code above to do what I need in my post above?
SambaNeko
Nov 23rd, 2009, 10:46 AM
Guys, have you not understood from the start what this code does?... It's really not "tracking" anything - it's just showing a directory structure from the root ("HOME") to your current position. Here is the page on my website:
.com/misc/vbforums/backlinks.php (http://www.spacecatsamba.com/misc/vbforums/backlinks.php) (abbreviated URL so it doesn't blank out the path with an ellipsis...)
It shows "HOME > Misc > Vbforums > Backlinks" because that matches the directory structure. All I did with the modified code was add the current page to the end of the breadcrumb trail.
If you're looking for true tracking, this code isn't it. If you want to ask questions about this code though, please include two things:
1.) The URL of the page you're on (like ".com/misc/vbforums/backlinks.php").
2.) The breadcrumb string you get (like "HOME > Misc > Vbforums > Backlinks").
Nightwalker83
Nov 23rd, 2009, 04:04 PM
Guys, have you not understood from the start what this code does?... It's really not "tracking" anything - it's just showing a directory structure from the root ("HOME") to your current position. Here is the page on my website:
Ah ok! Guess I will have to search some more in google are use html to accomplish what I'm after.
Edit:
This is the code I use:
<?php
function breadcrumb(array $options = array()) {
/*
* Setup the $options array with default values if none were
* specified
*/
if (isset($options['uri'])) $uri = $options['uri'];
elseif (isset($_SERVER['REQUEST_URI'])) $uri = $_SERVER['REQUEST_URI'];
else $uri = '';
if (isset($options['script_path'])) $script_path = $options['script_path'];
elseif (isset($_SERVER['SCRIPT_NAME'])) $script_path = $_SERVER['SCRIPT_NAME'];
else $script_path = '';
if (isset($options['server'])) $server = $options['server'];
elseif (isset($_SERVER['HTTP_HOST'])) $server = $_SERVER['HTTP_HOST'];
elseif (isset($_SERVER['SERVER_NAME'])) $server = $_SERVER['SERVER_NAME'];
else $server = '';
if (isset($options['protocol'])) $protocol = $options['protocol'];
elseif (isset($_SERVER['SERVER_PROTOCOL'])) {
if (stripos($_SERVER['SERVER_PROTOCOL'], 'https') !== false) $protocol = 'https';
else $protocol = 'http';
}
else $protocol = 'http';
if (isset($options['base'])) $base = $options['base'];
else $base = '';
if (isset($options['tpl'])) $tpl = $options['tpl'];
else $tpl = '<a href="%s">%s</a>';
if (isset($options['first_tpl'])) $first_tpl = $options['first_tpl'];
else $first_tpl = $tpl;
if (isset($options['last_tpl'])) $last_tpl = $options['last_tpl'];
else $last_tpl = $tpl;
if (isset($options['separator'])) $separator = $options['separator'];
else $separator = ' » ';
if (isset($options['home_link'])) $home_link = $options['home_link'];
else $home_link = 'Home';
if (isset($options['rm_file_ext'])) $rm_file_ext = $options['rm_file_ext'];
else $rm_file_ext = true;
if (isset($options['rm_path_info'])) $rm_path_info = $options['rm_path_info'];
else $rm_path_info = false;
if (isset($options['rm_query_string'])) $rm_query_string = $options['rm_query_string'];
else $rm_query_string = false;
if (isset($options['format_callback'])) $format_callback = $options['format_callback'];
else $format_callback = create_function('$s', 'return str_replace(array(\'-\', \'_\'),\' \',ucwords($s));');
/*
* Generate the breadcrumb
*/
$base = trim($base, '/');
if ($server) $path = $protocol.'://'.$server.($base ? '/'.$base.'/' : '');
else $path = $base;
$script_path = trim($script_path, '/');
$path_parts = explode('/', $script_path);
$output = '';
$output .= sprintf($first_tpl, $path, $home_link);
$i = 0;
while ($i < count($path_parts)) {
$path .= '/'.$path_parts[$i];
if (isset($path_parts[$i + 1])) {
//Comment out the following line to remove folders from the breadcrums
//$output .= $separator.sprintf($tpl, $path, $format_callback(basename($path)));
}
else {
$last_link = basename($path);
if ($rm_file_ext) {
$last_link = substr($last_link, 0, strrpos($last_link, '.'));
}
if (!$rm_path_info) {
$start = strlen($script_path) + 1;
$has_query_string = strpos($uri, '?');
if ($has_query_string) {
$end = $has_query_string - $start;
$path .= substr($uri, $start, $end);
}
else {
$path .= substr($uri, $start);
}
}
if (!$rm_query_string) {
$path .= '?'.(isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '');
}
//Set $format_callback($last_link) to "" if breadcrumb on home page
$output .= $separator.sprintf($last_tpl, $path, $format_callback($last_link));
}
++$i;
}
return $output;
}
The original code can be found here (http://www.htmlforums.com/serverside-scripting-tutorials/t-making-a-breadcrumb-in-php-85247.html)!
menre
Dec 6th, 2009, 01:51 PM
Hi,
Thanks for your help. My pages are not consistent the way they show the folder structure.
If you want to ask questions about this code though, please include two things:
1.) The URL of the page you're on (like ".com/misc/vbforums/backlinks.php").
2.) The breadcrumb string you get (like "HOME > Misc > Vbforums > Backlinks").
I am still working on my pages and will let you see how it is displayed. I will include the URL later please.
Thanks,
Menre
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.