28th Nov 2006
How to make a great ‘Go Back’ feature
You may or may not know about the PHP $_SERVER variable, HTTP_REFERER. It is meant to hold the URL of the previously visited page that the user has been to. Also, you may already use this variable in buttons or links to allow the user to “Go Back” to their previous page.
When I mentioned that the variable is meant to hold the data, unfortunately this is not always the case. A number of firewalls and other software stop the HTTP_REFERER header from being sent to the server, making any “Go Back” buttons or links on your site useless if you’re referring to this variable (Kerio Personal Firewall is one such application).
A recent look at my own logs tells me that over 25% of the users who download from this site hide their referring address. If you apply the same percentage to your site, that’s a lot of people who go nowhere when they click “Go Back”.
So what do you do? Save the current page to the $_SESSION array!
“But what a pain! having to do this for every page.”
Luckily for you I agree. So I’ve created a file you can include in your scripts to keep track of the users browsing history - automatically!
The script presents 2 constants to you: GOBACK and HISTORYWASPOSTED. GOBACK holds the URL of the previously visited page, and HISTORYWASPOSTED is either set to True or False depending if the user has reached that referring page by use of a POST form or not. Depending on the structure of your site, you’ll have to decide what the HISTORYWASPOSTED constant can do for you. You may not want a user to go back if they arrived at that page by a form. Your script might break without the right POST vars.
Finally, you can specify the history length at the top of the script.
<?php
//----------------------------------------------------------------
// History - by JC
// www.jc21.com
//----------------------------------------------------------------
// Keeps a track of http referers even for those users that
// disable referring (by firewall software or whatever).
//
// Usage: just include this file at the top of your script.
// The constant variable GOBACK holds the referring page.
// If that page was reached by a POST form submission, the constant
// variable HISTORYWASPOSTED will be True.
//----------------------------------------------------------------
//Number of history entries. Change this to your liking.
define('HISTORY_MAX',20);
//Supress errors incase session has already been started.
@session_start();
// Check if here by post
if (count($_POST) > 0) {
$_hbp = true;
} else {
$_hbp = false;
}
// Check if history array exists and create if not.
if (!is_array($_SESSION['_HISTORY'])) {
$_SESSION['_HISTORY'] = array();
}
//Check if this page is already on top
if ($_SESSION['_HISTORY'][1][0] == $_SERVER['REQUEST_URI'] && !$_SESSION['_HISTORY'][1][1]) {
//We're going back trough our history!
//Remove the first element of the array
array_shift($_SESSION['_HISTORY']);
} elseif ($_SESSION['_HISTORY'][0][0] != $_SERVER['REQUEST_URI'] || ($_SESSION['_HISTORY'][0][0] == $_SERVER['REQUEST_URI'] && $_hbp)) {
// Set this page as a referer.
array_unshift($_SESSION['_HISTORY'],array($_SERVER['REQUEST_URI'],$_hbp));
}
//Trim array to x many history entries.
if (count($_SESSION['_HISTORY']) > (HISTORY_MAX+1)) {
// get number of extras.
$_e = (count($_SESSION['_HISTORY']) - (HISTORY_MAX+1));
for ($x=0;$x<$_e;$x++) {
array_pop($_SESSION['_HISTORY']);
}
}
// Set GOBACK and HISTORYWASPOSTED
if (isset($_SESSION['_HISTORY'][0])) {
define('GOBACK',$_SESSION['_HISTORY'][1][0]);
define('HISTORYWASPOSTED',$_SESSION['_HISTORY'][1][1]);
} else {
define('GOBACK',false);
define('HISTORYWASPOSTED',false);
}
// Cleanup vars.
unset($_e,$_hbp);
?>
Download this code: history.inc.php
And here’s how you might use it:
<?php
include('history.inc.php');
?>
<p>Hello World</p>
<?php
//check if referring page wasn't result of a POST
if (!HISTORYWASPOSTED) {
?>
<p><a href="<?php echo GOBACK; ?>">Go Back</a></p>
<?php
}
?>
Download this code: history_use.php
You may or may not know about the PHP $_SERVER variable, HTTP_REFERER. It is meant to hold the URL of the previously visited page that the user has been to. Also, you may already use this variable in buttons or links to allow the user to “Go Back” to their previous page.
When I mentioned that the variable is meant to hold the data, unfortunately this is not always the case. A number of firewalls and other software stop the HTTP_REFERER header from being sent to the server, making any “Go Back” buttons or links on your site useless if you’re referring to this variable (Kerio Personal Firewall is one such application).
A recent look at my own logs tells me that over 25% of the users who download from this site hide their referring address. If you apply the same percentage to your site, that’s a lot of people who go nowhere when they click “Go Back”.
So what do you do? Save the current page to the $_SESSION array!
“But what a pain! having to do this for every page.”
Luckily for you I agree. So I’ve created a file you can include in your scripts to keep track of the users browsing history - automatically!
The script presents 2 constants to you: GOBACK and HISTORYWASPOSTED. GOBACK holds the URL of the previously visited page, and HISTORYWASPOSTED is either set to True or False depending if the user has reached that referring page by use of a POST form or not. Depending on the structure of your site, you’ll have to decide what the HISTORYWASPOSTED constant can do for you. You may not want a user to go back if they arrived at that page by a form. Your script might break without the right POST vars.
Finally, you can specify the history length at the top of the script.
<?php //---------------------------------------------------------------- // History - by JC // www.jc21.com //---------------------------------------------------------------- // Keeps a track of http referers even for those users that // disable referring (by firewall software or whatever). // // Usage: just include this file at the top of your script. // The constant variable GOBACK holds the referring page. // If that page was reached by a POST form submission, the constant // variable HISTORYWASPOSTED will be True. //---------------------------------------------------------------- //Number of history entries. Change this to your liking. define('HISTORY_MAX',20); //Supress errors incase session has already been started. @session_start(); // Check if here by post if (count($_POST) > 0) { $_hbp = true; } else { $_hbp = false; } // Check if history array exists and create if not. if (!is_array($_SESSION['_HISTORY'])) { $_SESSION['_HISTORY'] = array(); } //Check if this page is already on top if ($_SESSION['_HISTORY'][1][0] == $_SERVER['REQUEST_URI'] && !$_SESSION['_HISTORY'][1][1]) { //We're going back trough our history! //Remove the first element of the array array_shift($_SESSION['_HISTORY']); } elseif ($_SESSION['_HISTORY'][0][0] != $_SERVER['REQUEST_URI'] || ($_SESSION['_HISTORY'][0][0] == $_SERVER['REQUEST_URI'] && $_hbp)) { // Set this page as a referer. array_unshift($_SESSION['_HISTORY'],array($_SERVER['REQUEST_URI'],$_hbp)); } //Trim array to x many history entries. if (count($_SESSION['_HISTORY']) > (HISTORY_MAX+1)) { // get number of extras. $_e = (count($_SESSION['_HISTORY']) - (HISTORY_MAX+1)); for ($x=0;$x<$_e;$x++) { array_pop($_SESSION['_HISTORY']); } } // Set GOBACK and HISTORYWASPOSTED if (isset($_SESSION['_HISTORY'][0])) { define('GOBACK',$_SESSION['_HISTORY'][1][0]); define('HISTORYWASPOSTED',$_SESSION['_HISTORY'][1][1]); } else { define('GOBACK',false); define('HISTORYWASPOSTED',false); } // Cleanup vars. unset($_e,$_hbp); ?>
Download this code: history.inc.php
And here’s how you might use it:
<?php include('history.inc.php'); ?> <p>Hello World</p> <?php //check if referring page wasn't result of a POST if (!HISTORYWASPOSTED) { ?> <p><a href="<?php echo GOBACK; ?>">Go Back</a></p> <?php } ?>
Download this code: history_use.php
Posted in PHP | 2 Comments »