Archive for November, 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

Are you linked in?

I was browsing for the best web 2.0 apps recently, when I found a category winner: Linked In.

I’m not much for signing up for fad apps all the time, but I was interested in the connections that Linked In claimed to offer. It’s difficult to explain the overall purpose of the site, but here goes:

  • People Search – Name search, find an employer or future employee
  • Keep in touch for former colleagues or classmates
  • Use Outlook to help track people in your Linked In account

The coolest feature I found was a search for people you went to university with that are also linked in. Helping you find out how your long lost buddies are doing these days (or how you can do better).

Sure enough, after uploading my address book I found that there two of my friends are also Linked In. Through those contacts, I am also linked to more than 300 other professionals. I don’t know what I’m going to do with those connections yet… Sometimes it’s not what you know it’s who you know (and who they know!).

The New Lightbox 2

As I stumble through the web today, I found a new version of the famous Lightbox JS.

A quick look at the examples and I was impressed with the improvements. I know how hard it is to fade an entire page without flickering (actually, I haven’t worked that out yet!).

onChange vs onClick Event

Recently I was working on a piece of code that would show or hide particular elements based on a Radio option’s ‘checked’ property.

I used the onChange event to fire my code, because that’s what I was doing to the radio option, right?

In a way yes it worked – in every browser but IE. After reading a post about browser inconsistencies I learned that IE doesn’t fire the onChange event, until after I lose focus of the option I just changed – which just doesn’t make sense!

Here’s a quick example of what I’m talking about. Try this in different browsers.

Needless to say, using onClick instead fixed the problem. It seems that onChange still exists for the user who sometimes prefers not to use a mouse when navigating a website. For the purposes of the few, I try to use both events (providing the function to be called checks the status of things to avoid double handling).

PHP Ajax File Browser 2.0.1

A new version of PHP Ajax File Browser is now available. It fixes two major problems:

  • Fixed normal user account admin priveleges bug
  • Fixed accented characters prevented from listing. Still has some display problems in IE6.

Download

Technorati: ajax, php, javascript, web 2.0

Snippets Online

Today I launch another small database site.

Snippets.jc21.com is a database of useful pieces of code from any langauge you may use.

I often use different computers to code, or sometimes forget to backup my snippets before a re-format. So I made this site to keep a track of things.

The site allows full search and browsing capabilities, so you can easily find what you’re looking for. There are only a few gems there at the moment, but will be growing each day.
If you have any useful snippets or functions that you would like to share, submit them.