我可以以编程方式将用户从.htaccess授权中注销吗


Can I programmatically log a user out of .htaccess authorization?

我使用Apache"Auth"安全性来限制对我的网站的访问(通过.htaccess文件、.htpasswd文件等中的命令)。

有没有一种方法可以通过我的PHP脚本取消用户的授权,有效地为他们提供注销的方法?

使用这种类型的身份验证,浏览器实际上会在每次后续请求时发送用户名和密码。由于没有办法告诉浏览器"嘿,停止发送这些",也没有办法做你想做的事情。

(但是,如果您有一个涉及处理部分身份验证的PHP脚本,您可以设置一个用于标记的会话变量,以忽略有效的身份验证并假装用户已注销。)

然而,就好的解决方案而言,没有一个。用户将保持登录状态,直到他或她的浏览器决定停止发送标题(通常是在浏览器关闭时)。

<?
// this PHP will cause a logout event, and give the login prompt again
$AuthName='WHAT-EVER'; // must match AuthName in .htaccess.
header('HTTP/1.0 401 Unauthorized');
header('Content-type: text/html');
header('WWW-Authenticate: Basic realm="'.$AuthName.'"');
// now redirect them when they click cancel
// should be to a page with no password required.
// use an HTML meta redirect instead of HTTP 
// so it runs after the auth is cancelled.
?>
<html><head><meta http-equiv='refresh' content='0;../'></head></html>

这就是您想要的吗?

http://www.php.net/manual/en/features.http-auth.php#99348