views:

120

answers:

2

Is there any way by which I can attach the back button of my browser to any particular link?

Actually I am designing a login page in PHP. After login I want the back button of my browser to open any particular page that I want, how can I do that in PHP?

Actually I am not a PHP guy, so my question might sound silly to some. :P

+2  A: 

I suspect you want to redirect the user to a particular page after he logs in, you can simply use the header function for that:

header("LOCATION: user-panel.php");

That will redirect home to user-panel.php page.

The browser's back button goes back until there is history found, just to add that you can use javascript for that although this might not be required in your case:

<a href="#" onclick="history.back(); return false;">Go Back</a>

More info here

Update Based On Comment:

Basically you set a session when the user is authenticated for the first time, here is an example:

session_start();

// check if the user is already logged in: if yes redirect him even if the back button is clicked

if (isset($_SESSION['logged']))
{
    header("LOCATION: user-panel.php");
}

// below is your own normal code

// your db query if the user specified criteria was met

if (user found)
{
  $_SESSION['logged'] = true; // you should add this line if not already there
  // redirect the user
}
Sarfraz
You did not get me. I have already written code for redirection but when I click the back button of the redirected page the login page gets displayed even though I have already logged in.
Prasoon Saurav
@Prasoon Saurav: I have updated my answer. Let me know if you have still any confusion.
Sarfraz
A: 

As @Sarfraz correctly says header is the way to go. The back button is on the browser. PHP runs on the server, it does know anything about what happens in the client's browser. The page may as well have been accessed from a shell, for instance, where you have no back button.

Also, that would not be good page design, as people expect the page to rediret automagically after a login, not to have to push the back button.

nico