简体   繁体   中英

Pressing back button redirects the logged user to log out?

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM