Subscribe to Feeds
09 Jan

Back to Previous Page (PHP)

Resources

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.

End of Article

22 Responses to “Back to Previous Page (PHP)”

[3] 2 1 » Show All

  • a gravatar

    t3rry Said:  

    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

    22
  • a gravatar

    Keith Gardner Said:  

    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.

    21

Trackback / Pingbacks

Leave a comment

Small Life

Naturaaa

Droplet

Chameleon

Snail

© 2007 - 2008 Dezinerfolio. All Rights Reserved.
Powered by Wordpress & Hosted at Zone.net