Save 25% on Wizard’s SQL for WP w/ code: WIZARDSQL
Web Dev + WordPress + Security

Go Back via JavaScript and PHP

This quick tutorial explains how to use HTML, JavaScript, and/or PHP to enable visitors to “go back” to the previous page. You can use either method to add a simple “go back” link or form button to your web pages.

Tip: Check out this PHP code snippet, back to history button. Thanks to thisbit.

“Go back” link via HTML & JavaScript

Use this simple code as a button that will return users to the previous page:

<form>
	<input type="button" value="Return to previous page" onClick="javascript:history.go(-1)" />
</form>

Here it is as a simple text link:

<p>
	<a href="javascript:history.go(-1)" title="Return to the previous page">« Go back</a>
</p>

You can customize the text and attributes as desired.

“Go back” link via PHP, HTML, & JavaScript

You can make things easier by serving PHP and printing the link automatically. Here is the button link:

echo '<form><input type="button" value="Return to previous page" onClick="javascript:history.go(-1)"></form>';

And here is the PHP code to print a “Go back” text link:

echo '<p><a href="javascript:history.go(-1)" title="Return to previous page">« Go back</a></p>';

As before, customize the text and attributes as desired.

Advanced “go back” technique with PHP

Better yet, you can kick the accessibility factor up a notch by using PHP’s global HTTP_REFERER variable to write explicitly the previous URL, thereby eliminating the JavaScript requirement (thanks to Rick Beckman for the idea):

<?php $referer = filter_var($_SERVER['HTTP_REFERER'], FILTER_VALIDATE_URL);
	
	if (!empty($referer)) {
		
		echo '<p><a href="'. $referer .'" title="Return to the previous page">« Go back</a></p>';
		
	} else {
		
		echo '<p><a href="javascript:history.go(-1)" title="Return to the previous page">« Go back</a></p>';
		
	}
?>

The previous code will create an explicit “Go back” link when the referring URL is known. In those awkward situations where no referring URL has been recognized, the function writes a “Go back” link via JavaScript.

Important: In the previous code example, make sure to add proper sanitization for the filter_var() function, for example by using the proper filter flags.

Bonus: Go back technique for WordPress

Using WordPress? Here is how to add a “go back” snippet via custom code:

$referer = (wp_get_referer()) ? wp_validate_redirect(wp_get_referer()) : get_home_url();
		
if (!empty($referer)) {
	
	echo '<a href="'. $referer .'" title="Return to the previous page">Go back</a>';
	
	} else {
	
	?>
	
	<script type="text/javascript">
		function backClick(event) {
			if (document.referrer.indexOf(window.location.host) !== -1) {
				history.go(-1); return false;
			} else { 
				event.preventDefault();
			}
		}
	</script>
	
	<?php echo '<a onclick="backClick()" title="Return to the previous page" onMouseOver="this.style.cursor=\'pointer\'">Go back</a>'; 
	
}

This snippet adapted from thisbit gist.

Jeff Starr
About the Author
Jeff Starr = Designer. Developer. Producer. Writer. Editor. Etc.
Digging Into WordPress: Take your WordPress skills to the next level.

5 responses to “Go Back via JavaScript and PHP”

  1. Avatar photo
    Rick Beckman 2007/03/12 2:34 pm

    If you are using PHP for it, wouldn’t it be easier to catch the referral URL, if present, and then presenting the link with an actual URL pointer, rather than JavaScript? Accessibility would be helped quite a bit then.

    If no referral URL is set (some users turn it off), a JavaScript-based fallback could be used.

  2. Avatar photo
    Perishable 2007/03/12 3:08 pm

    Of course, of course … I was just testing you ;)

    Check out the new, improved version of the article to see your idea in action.

    – Thanks for the catch!

  3. Avatar photo
    Rick Beckman 2007/03/12 3:49 pm

    You’re welcome!

  4. thank you for the info! works fine…

  5. Avatar photo
    Perishable 2007/09/22 8:59 pm

    Happy to help!

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
GA Pro: Add Google Analytics to WordPress like a pro.
Thoughts
Plugin updates! All of our free and pro plugins ready for WordPress 6.2.
Daylight savings is a complete waste of time and needs to be eliminated.
Got a couple of snow days here in mid-March. Fortunately it's not sticking.
I handle all email in real time as it comes in, perpetually clear inbox for years now.
Added some nice features to Wutsearch search engine launchpad. Now 21 engines!
.wp TLD plz :)
Nice collection of free SEO APIs and user-agent lookups for Googlebot, Bingbot, Applebot, YandexBot, and more.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.