htpasswd -c /path/to/.htpasswd username
<?php
$htpasswd = '/path/to/.htpasswd'; // .htpasswd文件的路径
$realm = 'Restricted Area'; // 认证区域的名称
// 认证用户
function authenticate() {
header('WWW-Authenticate: Basic realm="' . $GLOBALS['realm'] . '"');
header('HTTP/1.0 401 Unauthorized');
echo 'Authorization Required.';
exit;
}
// 检查认证信息
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
authenticate();
} else {
$fp = fopen($GLOBALS['htpasswd'], 'r');
while (!feof($fp)) {
$line = trim(fgets($fp));
if (!empty($line)) {
list($user, $pass) = explode(':', $line);
if ($_SERVER['PHP_AUTH_USER'] == $user && md5($_SERVER['PHP_AUTH_PW']) == $pass) {
fclose($fp);
exit;
}
}
}
fclose($fp);
authenticate();
}
?>
PHP------网站后台加固
共计 0 条评论,点此发表评论