Skip to content

Web notes

HTTP auth logout method

HTTP basic auth has no logout button. A short PHP response can force the browser to ask for credentials again.

By Jozsef 2 min read

HTTP basic auth has no built in logout. The browser keeps the credentials and sends them again on the next request. This small PHP response gives the user a logout feeling: the browser asks for the username and password again.

php
<?php
  header('WWW-Authenticate: Basic realm="protected area"');
  header('HTTP/1.0 401 Unauthorized');
?>

I use it with a logout controller. The controller sets a session variable and redirects to the home page. The home page checks the variable, sends these headers, and clears the variable.

This is my method. Use it as a starting point, not as a replacement for a real session based login.

Sources and further reading