PHP:fopen检查客户端是否已使用受密码保护的文件进行身份验证(htpasswd)

时间:2012-03-14 02:49:20

标签: php apache .htpasswd

假设我使用名为/ secret

的htpasswd保护目录

有效用户登录/ secret。

将它们重定向回公共区域。

在公共区域内,有没有办法在PHP中知道当前用户是否已通过htpasswd进行身份验证?

谢谢你的时间!

1 个答案:

答案 0 :(得分:3)

在/ secret文件夹中,您可以让索引页面设置会话并从公共区域检查。

例如,在PHP中:

/secret/index.php

<?php
session_start();
$_SESSION['htpasswdAuth'] = true;
header("Location: /public/area");
?>

然后您的其他脚本可以执行以下操作:

<?php
session_start();

if(isset($_SESSION['htpasswdAuth']) && $_SESSION['htpasswdAuth'] == true)
{
    echo 'hello authenticated user!';
}
?>