What is the correct way to log out of HTTP authentication protected folder?
There are workarounds that can achieve this, but they are potentially dangerous because they can be buggy or don't work in certain situations / browsers. That is why I am looking for correct and clean solution.
Logout from HTTP Basic Auth in two steps
Let’s say I have a HTTP Basic Auth realm named “Password protected”, and Bob is logged in. To log out I make 2 AJAX requests:
WWW-Authenticate: Basic realm="Password protected"
At this point browser forgot Bob’s credentials.
The only effective way I've found to wipe out the
PHP_AUTH_DIGEST
orPHP_AUTH_USER
ANDPHP_AUTH_PW
credentials is to call the headerHTTP/1.1 401 Unauthorized
.Workaround (not a clean, nice (or even working! see comments) solution):
Disable his credentials one time.
You can move your HTTP authentication logic to PHP by sending the appropriate headers (if not logged in):
And parsing the input with:
So disabling his credentials one time should be trivial.
My solution to the problem is the following. You can find the function
http_digest_parse
,$realm
and$users
in the second example of this page: http://php.net/manual/en/features.http-auth.php.There's a lot of great - complex - answers here. In my particular case i found a clean and simple fix for the logout. I have yet to test in Edge. On my page that I have logged in to, I have placed a logout link similar to this:
And in the head of that logout.html page (which is also protected by the .htaccess) I have a page refresh similar to this:
Where you would leave the words "logout" in place to clear the username and password cached for the site.
I will admit that if multiple pages needed to be able to be directly logged in to from the beginning, each of those points of entry would need their own corresponding logout.html page. Otherwise you could centralize the logout by introducing an additional gatekeeper step into the process before the actual login prompt, requiring entry of a phrase to reach a destination of login.