Windows IIS 7.5上的PHP领域认证
问题描述:
我需要在运行于Windows IIS 7.5 Web服务器上的PHP网站上的文件夹上使用领域认证。我在下面的代码中,该目录中的任何.php
文件都很好。Windows IIS 7.5上的PHP领域认证
问题是我需要密码保护访问整个目录,包括PDF,图像文件,CSS文件等我不能将PHP代码放在这些类型的文件。
我确实在服务器上安装了IIS重写模块,所以我假设我可以通过某种方式在我的web.config中添加一个重写,这可以通过某种传递/处理程序PHP文件强制所有文件。
我只是不知道如何做到这一点。
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Jonas Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'User pressed Cancel';
exit;
} else {
echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as you password.</p>";
}
?>
答
我回复了similar question不同的是,你正在使用ISS,而不是Apache和要提供的所有文件,而不仅仅是几个类型。您可以复制<rule>
标签。此规则将匹配除php
分机外的所有文件(请勿negate=true
)。您必须将其更改为您的特定要求。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="false" />
<rewrite>
<rules>
<rule name="Authentication" stopProcessing="false">
<match url="^(.*)$" negate="false" />
<action type="Rewrite" url="validate.php?path={R:0}" appendQueryString="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
<add input="{QUERY_STRING}" pattern="^(.*\.php)$" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
这里是一个不同版本的文件isAuthenticated.php
,我用和使用位,你发布的代码的。
$blacklistExtensions = array();
$path = $_SERVER["DOCUMENT_ROOT"].DIRECTORY_SEPARATOR.$_REQUEST["path"];
$pathInfo = pathinfo($path);
if(in_array($pathInfo["extension"], $blacklistExtensions)) {
header("HTTP/1.1 403 Forbidden");
exit;
}
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Jonas Realm"');
header('HTTP/1.0 401 Unauthorized');
exit;
}
if(!file_exists($path)) {
header("HTTP/1.1 404 Not Found");
exit;
}
// Display the file and set the correct mimetype
$resource = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($resource, $path);
finfo_close($resource);
header("Content-Type: ".$mimetype);
readfile($path);
希望这可以让你开始为寻找一个解决这个问题:)