TrailScout (PHP)

A cookie-based PHP script that displays a path of hyperlinks to previously visited pages on a website. For example "Vacilando.Org > Writing > PHP Scripts > TrailScout". It is also known as "cookie crumb trail". None of the similar programs I found around the Web was good enough, so I decided to write my own.

This system does not need to use a database. The path is stored in a cookie for the duration of one session (= until you close all your browser windows). For each new visited page, the title (fetched automatically from between your tags) and the URL (complete with parameters) is stored in that session variable. If user visits a page that s/he has seen before, the trail of links is reduced and that page again becomes the last one in a row. There is a variable that can be used to limit the maximum number of links to be stored (this is useful for large sites where trails tend to be too long).

Using the script below, to print the crumb path use simply:

echo $trailscout_show;

<?php
/*************************************************************************************************************************
*
* Name: TrailScout
* Code developed by: Tomas J. Fulopp ( http://vacilando.net/tf )
* Source code: http://vacilando.net/node/264603
* First created: 20020215. Last update: 20060125, 20091213. Version: 2.1
* If you have any comment, bug report, or question, please don't hesitate to contact me: i n f o AT vacilando DOT n e t
* 
* A cookie-based PHP script that displays a path of hyperlinks to previously visited pages on a website. For example "vacilando.org > Writing > PHP Scripts > TrailScout". It is also known as "cookie crumb trail". None of the similar programs I've found around the Web was good enough, so I decided to write my own. The path is stored in a cookie for the duration of one session (= until you close all your browser windows). For each new visited page, the title (fetched automatically from between your <TITLE></TITLE> tags) and the url (complete with parameters) is stored in that session variable. If user visits a page that s/he has seen before, the trail of links is reduced and that page again becomes the last one in a row. There is a variable that can be used to limit the maximum number of links to be stored (this is useful for large sites where trails tend to be too long).
* 
* Save the whole script as e.g. "trailscout.php". You can then include it in your PHP pages using:
* $relroot = ""; // relative path to the folder where this script is located, e.g. "../scripts/"
* include ($relroot . 'trailscout.php');
* and the click path can then be displayed by
* echo $trailscout_show;
* 
* Make sure that TrailScout is the first script to run on your page; since the cookie needs to be set, it is vital that this program is run before any HTML code is output to the website.
* 
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. The GNU General Public License is to be found at http://www.gnu.org/licenses/gpl.txt In case you do not find it, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
* 
*************************************************************************************************************************/

# First we need to get the title of the page we are visiting (i.e. what is betweeen <title></title>
$thispage = str_replace("/", "", $_SERVER["PHP_SELF"]); # This works for pages in the web root. For pages in folders you have to adapt it, e.g. to $thispage = SUBSTR($_SERVER["PHP_SELF"], STRPOS($_SERVER["PHP_SELF"], "/", 2) +1 );
$fp = fopen( $thispage, 'r');
while ( !feof($fp) )
{
  $stuff .= fgets($fp, 1024);
  if ( stristr($stuff, '</title>' )) { break; }
}
eregi("<title>(.*)</title>", $stuff, $title );
$title = $title<span className="dropcap">1</span>;

/*
Here starts the TrailScout code itself. First we'll make an element to store in the cookie, consisting of two parts:
the script name and path (with all parameters), delimiter (~), then the page title, and then element delimiter (^)
E.g.: "/travel/photos.php?go=11~Travel Photographs^"
*/

// Maximum number of past links in the trail.
$maxlinks = 17;
// My domain
$mydomain = "vacilando.org";

$params = $_SERVER["QUERY_STRING"];
if ( $params <> "" ) { $params = "?" . $params; }
// Remove all kinds of quotation marks from the title that goes into cookie. Otherwise malfunctions
$titleforcookie = str_replace("'", "%%%", $title);
$titleforcookie = str_replace('"', '%%%', $titleforcookie);
//echo $title;
$currentpage = $_SERVER["PHP_SELF"] . $params . "~" . trim( $titleforcookie ) . "^";

$trailscout = $_COOKIE['trailscout'];
if ( $trailscout == '' )
{
  $trailscout = $currentpage;
} else {
  if ( strstr( $trailscout, $currentpage ) <> false )  // Means we have been on that page already. Cut everything that follows it, from the trail string..
  {
    $pos = strpos( $trailscout, $currentpage );
    $len = $pos + strlen( $currentpage );
    $trailscout = substr( $trailscout, 0, $len );
  } else
  {
    // If there are more than $maxlinks then remove the first one.
    If ( substr_count ( $trailscout, '~' ) == $maxlinks )
    {
      $trailscout = strstr ( $trailscout, '^' );
      $trailscout = substr ( $trailscout, 1 ); // Removes initial '^'
    }
    $trailscout = $trailscout . $currentpage;
  }
}
setcookie ( "trailscout", $trailscout, '0', '/', '.' . $mydomain ); // Setting a session cookie (somehow, in IE, value had to be 0, although read that it should be nothing...)

/*
if ( !session_is_registered('trailscout') )
{
session_register('trailscout');
$trailscout = $currentpage;
} else {
if ( strstr( $trailscout, $currentpage ) ) {
$pos = strpos( $trailscout, $currentpage );
$len = $pos + strlen( $currentpage );
$trailscout = substr( $trailscout, 0, $len );
} else {
$trailscout = $trailscout . $currentpage;
}
}
*/

$line = explode( "^", $trailscout );
$item = explode( "~", $line<span className="dropcap">0</span> );
$trailscout_show = '<a href="' . $item<span className="dropcap">0</span> . '"><font color="';
$trailscout_show .= '#000000';
$trailscout_show .= '">' . $item<span className="dropcap">1</span> . '</FONT></a>';
$countline = count( $line ) - 1; // Min 1 because there's always a delimiter at the end but nothing after it.
if ( $countline > 1 )
{
  for ( $x = 1; $x <= $countline - 1; $x++ ) // Min 1 because array starts from zero.
  {
    $item = explode ( "~", $line[$x] );
    $trailscout_show .= ' <font color="';
    $trailscout_show .= '#000000';
    $trailscout_show .= '">></FONT> ' . '<a href="' . $item<span className="dropcap">0</span> . '"><font color="';
    $trailscout_show .= '#000000';
    $item<span className="dropcap">1</span> = str_replace("%%%", "'", $item<span className="dropcap">1</span>);
    $trailscout_show .= '">' . $item<span className="dropcap">1</span> . '</FONT></a>';
  }
}
?>```

Tomáš Fülöpp
Anderlecht, Belgium
February 15, 2002, January 25, 2006, December 13, 2009
Tomáš Fülöpp (2012)

Tagsxesterbreadcrumbphpnavigationwebsitehistoryproject
LanguageENGLISH Content typeARTICLELast updateOCTOBER 20, 2018 AT 01:46:40 UTC