I have a logging system with username and password, after the user logged in with the username and password, and if i press the back button it redirects to me to the log in page. How to prevent this in php??
-
Why would you want this...? – Alexis King Mar 01 '13 at 06:42
-
1I think you can perform a check in the login page: if this user is already logged in - redirect him somewhere else. – k102 Mar 01 '13 at 06:45
-
that is not my problem.. – AndRaGhu Mar 01 '13 at 06:48
3 Answers
First you want to make the pages expire and prevent them from being cached by the browser:
<?
//Set no caching
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
Second, you should check if the user is logged in, if so, redirect them back to the previous page, something like this:
// SET REFERRER
function strleft($s1, $s2) {
return substr($s1, 0, strpos($s1, $s2));
}
function selfURL() {
if(!isset($_SERVER['REQUEST_URI'])) {
$serverrequri = $_SERVER['PHP_SELF'];
}
else {
$serverrequri = $_SERVER['REQUEST_URI'];
}
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
$_SESSION['ref'] = $protocol."://>/".$_SERVER['SERVER_NAME'].$port.$serverrequri;
}
selfURL();
Then
header("Location: " . $_SESSION['ref']);

- 2,317
- 1
- 11
- 30
You have to check the session of the user. When then session exist give a warning that the user is already logged in or redirect the user to index.php or something.

- 2,607
- 3
- 31
- 55
-
-
1@AndRaGhu, you cannot control user's "back button" in php, since php is server side. Get yourself some book on web programming. – k102 Mar 01 '13 at 06:53
-
When the user is logged in, you need to redirect him to a page to prevent the re-submit warning. – Jordi Kroon Mar 01 '13 at 06:53
As per your comments you suggest the question you are really asking is how to disable the back button?
This is definitely not possible from php, and I think probably not possible at all as it is controlled by the browser. JavaScript gives you some control so you may find code to ignore Or disable the button.
Googling for 'JavaScript disable back button' should give you some results.

- 10,997
- 11
- 73
- 124