Dezinerfolio

Back to Previous Page (PHP)

Your rating: None Average: 3.9 (39 votes)

While working on one of our projects we came across a requirement which was basically a Link which takes you to the previous page within the same site.

We googled and found a lot of ways which were complex. After giving it a thought, we just figured out that its a very simple logic with a few lines of code.

Well, our requirement was for a Drupal site so got the logic on PHP. Here it goes [sharing is our policy] :

<?php session_start(); ?> <!-- Starting a session before the DOCTYPE -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<a href="<?php echo $_SESSION['back']; ?>">back</a> <!--The anchor tag that links to the previous page -->

<?php $_SESSION[’back’] = htmlentities($_SERVER[’REQUEST_URI’]); ?>  <!-- Assigning the current URL to a session variable -->

Please note that first we assign the previous session variable to the anchor tag and only then re-assign the current URL to the session variable which will be read when you move on to the next page.

This being a simple PHP snippet can be used in almost every CMS (PHP based) and sure is a good addon to the accessibility of your site.

Trackback URL for this post:

http://www.dezinerfolio.com/trackback/148

Comments

Anonymous's picture

It helped me very well:)

tomas drozda (not verified)
Tue, 08/09/2011 - 17:51
Anonymous's picture

Thanks ya.it's very nice articles....

geetha (not verified)
Sat, 03/12/2011 - 12:11
Anonymous's picture

What if the user refresh the page :O , the back would get set to the current page itself

Robin John (not verified)
Mon, 09/27/2010 - 20:07
Anonymous's picture

I put this in my 401 error page to enable me to include a 'retry' button in the event of entering the wrong username/password. It works great in firefox 3.5.4 - the retry button causes the password dialog box to reappear (what I wanted), but in IE8 I get sent back to the page previous to the one requiring authentication. I suppose the two browsers handle the authentication request differently and my setup is a bit of a hack ^^

Anonymous (not verified)
Sat, 10/31/2009 - 11:45
Anonymous's picture

The one problem with this method is if a user has multiple browsers open on the same site. The sessions get miffed up and copy over to each browser session.

Anonymous (not verified)
Thu, 12/18/2008 - 11:06
Anonymous's picture

This is really cool since IE has JS switched off by default.

This whole MS stupid game is utter madness and makes life hell for web designers/developers (esp. inexperienced ones like me).

Thanks to dezignerfolio and sites with a similar mentality of assisting each other, sanity is still alive and well on the net.

Thanks also for the dfGrid 1.0
Fascinating ... I must investigate prototype

cha
t3rry

t3rry (not verified)
Fri, 05/02/2008 - 03:01
Anonymous's picture

Hmmm, I'm not sure why Melanie Baker and I are having the same problem. I am clearly a neophyte, but I have done a little php coding before. Here however I just don't know what constitutes a "session." Am I to put all the code you indicated above into a single page of my Drupal site? Am I to put it on every page? Certain pages? Some of it on one page and some of it on the referred-to pages? I just don't know. But my "back" link keeps getting set to:
http://www.normanbobrow.com/”“
which is of course a page that doesn't exist.

Please if you could add a few more words of explanation, it would really help us out a lot. Thanks.

Keith Gardner (not verified)
Mon, 04/28/2008 - 05:15
Anonymous's picture

Hi i'm having trouble with this code, I keep getting the message : The requested URL /”“ was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Any suggestions?? Thanks

Melanie Baker (not verified)
Fri, 04/25/2008 - 10:18
Anonymous's picture

Good stuff. On a side note, I really dig your work, awesome style all around.

atom (not verified)
Thu, 01/10/2008 - 09:43
dfadmin's picture

@atom - totally agree, users can use htmlentities to strip the characters so that no script tag loads...
Thanks for the healthy arguments, this helps us too to understand concepts better.

I will rewrite the post adding html entities

Thu, 01/10/2008 - 06:32

Post new comment

The content of this field is kept private and will not be shown publicly.