HTTP身份验证清除SERVER变量

时间:2016-04-18 17:17:50

标签: php mysql http authentication

我有以下问题:

我使用HTTP身份验证登录。我在php结束会话,但用户和密码的变量仍然保持不关闭 网页浏览器。有没有办法在不关闭Web服务器的情况下清除这些变量。

我的测试网站:

1.- login.php (HTTP Authentication and mysql users data base);
2.- If ok user and password go to page1.php else kept prompting for user and password;
3.- In page1.php there is a link to logout.php which ends the php session. 
4.- If I click the button back to page1.php it tells me that I am logged out and gives me a link to login.php to login again.
5.- When click the link to login again there is not prompt for user and password it redirects me to page1.php and tells me that I am
    logged in with same user and password as the firs time.

提前感谢您的帮助。

thor6006

1 个答案:

答案 0 :(得分:0)

HTTP身份验证的一个主要问题是没有正确的方法强制浏览器将用户注销。不幸的是,甚至没有一种方法可以跨浏览器使用。

理想情况下,您将实施自己的身份验证系统,而不是依赖HTTP身份验证。

但是,可以让浏览器通过某些Javascript删除身份验证标头。

<html>
    <head>
        <script type="text/javascript">
            function logout() {

            var xmlhttp;
            var logout_redirect = "/where/to/redirect";
            var always_200 = "/path/that/will/return/200/OK";

            if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
            }  else if (window.ActiveXObject) {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }

            if (window.ActiveXObject) {
                // Handle IE
                document.execCommand("ClearAuthenticationCache");
                window.location.href = logout_redirect;
            } else {
                // Handle other browsers
                xmlhttp.open("GET", always_200, true, "logout", "logout");
                xmlhttp.send("");
                xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == 4) {
                        window.location.href = logout_redirect;
                    }
                }
            }
            return false;
        }
    </script>
</head>

<body>
    <a href="#" onclick="logout();">Log out</a>
</body>