Pressing back button redirects the logged user to log out?

a 夏天 提交于 2019-12-13 05:25:45

问题


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


回答1:


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:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/15152052/pressing-back-button-redirects-the-logged-user-to-log-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!