【问题标题】:HTTP Authentication clear SERVER variablesHTTP 身份验证清除 SERVER 变量
【发布时间】:2016-04-18 17:17:49
【问题描述】:

我有以下问题:

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

我的测试站点:

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.

提前感谢您的帮助。

雷神6006

【问题讨论】:

  • 浏览器将继续发送 http basic 的 auth/pass 直到浏览器被告知它们不再有效 - 例如。 Web 服务器必须发送 401/unauthorized 来欺骗浏览器认为凭据不再有效。
  • 感谢您的回答我使用这个:header("HTTP/1.1 401 Unauthorized");出口;没用

标签: php mysql http authentication


【解决方案1】:

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>

【讨论】:

    猜你喜欢
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多