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.
Comments
It helped me very well:)
Thanks ya.it's very nice articles....
What if the user refresh the page :O , the back would get set to the current page itself
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 ^^
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.
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
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.
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
Good stuff. On a side note, I really dig your work, awesome style all around.
@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
Post new comment