我试图通过Apache的autoindex module使用PHP在Apache中实现经过身份验证的文件列表.
我想象的方式是让Apache运行一个PHP脚本作为header file.我已经设法让Apache为头文件正确运行PHP,它也检测到登录cookie.但似乎Apache将头文件作为单独的请求运行,这意味着如果我尝试从PHP发送重定向头,则它不会运行.
我的(简化的)Apache配置:
DocumentRoot "/path/to/files_root"
Alias /~extra "/path/to/extra-data"
Options -Indexes -MultiViews +Includes
AllowOverride None
Order allow,deny
Allow from all
IndexOptions FancyIndexing HTMLTable SuppressHTMLPreamble
AddType text/html .php .html .htm
AddOutputFilter INCLUDES .php
AddHandler application/x-httpd-php .php
HeaderName "/~extra/HEADER.php"
我的HEADER.php文件:
if ( ! my_validate_cookie_function()) {
header('HTTP/1.1 302 Found');
header('Location: http://login.example.com/');
exit(1);
}
因此,标头不会发送到浏览器.设置Apache环境viariables似乎不起作用,因为它们在HEADER.php完成执行的那一刻就已经过去了.
cookie本身是加密的,因此需要PHP来验证它.
有什么建议如何达到预期的效果?
解决方法:
您应该使用< body>中的以下代码将index.php文件插入到您的目录中.标签.
function fileindex($folder) {
if (!is_dir($folder)) {
return array(); //empty if not a folder
}
$list = scandir($folder);
array_shift($list); //first two values are always . & ..
array_shift($list);
return $list;
}
/* auth stuff here */
if (is_auth) {
echo "
Index of ".getcwd()."
\n- ";
echo "\n
Parent Directory";foreach (fileindex(".") as $i) {
echo "\n
".htmlentities($i, ENT_QUOTES|"ENT_HTML401", "UTF-8", true)."";}
echo "
";}
既然你告诉我你不能使用index.php,你应该使用Apache将目录重定向到wherever / other.php?directory = path并从那里开始工作.
在.htaccess中,解决方案就是
RewriteCond %{REQUEST_URI} -d
RewriteRule ^(.*)$wherever/other.php?directory=$1 [L]
然而,最值得注意的是,您需要稍微编辑PHP代码以适应作为$_GET参数的文件夹,而不是getcwd()和fileindex(“.”).
标签:apache,php,redirect
来源: https://codeday.me/bug/20190630/1333217.html