0

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??

AndRaGhu
  • 161
  • 7
  • 17

3 Answers3

4

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']);

Phillip Berger
  • 2,317
  • 1
  • 11
  • 30
0

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.

Jordi Kroon
  • 2,607
  • 3
  • 31
  • 55
0

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.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124