Attaching a link to back button in php
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?
Actu开发者_Go百科ally I am not a PHP guy, so my question might sound silly to some. :P
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
}
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.
精彩评论