HTTP authentication logout via PHP

To log out a user from HTTP authentication using PHP, you can use the header function to send a "WWW-Authenticate" header with a value of "Basic realm=realmName" and a 401 status code. This will cause the browser to prompt the user to enter their credentials again.

Here's an example of how you can log out a user using this method:


header('WWW-Authenticate: Basic realm="realmName"');
header('HTTP/1.0 401 Unauthorized');

Watch a course Learn object oriented PHP

You should also clear any session data that you may have stored for the user, to ensure that the user is logged out from the application as well.