好吧,这里是我的回答 - 这是真的,最好的答案是“没有'。但是图像/ js/css在开发过程中比较重要(在实时公开之前)以及客户端预览指示我们不能执行基于IP的apache规则。因此,(,稍作修改,从上面)的规则是
RewriteEngine On
# Exclude the public and error directories from authentication
RewriteRule ^(public|error)($|/) - [L]
# Perform authentication via php
RewriteCond %{REQUEST_FILENAME} !auth.php
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* auth.php?requested_file=$0 [QSA,L]
(因为我需要几个子目录,其中内容确实是公开的,写着:在登录页上使用的图片/ CSS/JS)
和相关的PHP如下;对于
if($authenticated){
if($extension == 'php'){
call_user_func(function(){
$file_name = func_get_arg(0);
$path = getcwd();
chdir(pathinfo($file_name,PATHINFO_DIRNAME));
return include($file_name);
chdir($path);
}, $file_name);
} else {
//set cache headers so the browsers don't have to refresh all the
// static content
header_remove('X-Powered-By');
header_remove('Transfer-Encoding');
header_remove('Cache-Control');
header_remove('Pragma');
header_remove('Expires');
//header('Expires:');
header('Content-type: '.$mime_type);
readfile($file_name);
}
}
这样做是执行使用call_user_func()
停止命名空间污染,include()
执行PHP PHP和chdir()
以确保脚本得到正确的当前工作目录。
这是'容易'的一部分;内容头文件和MIME类型必须被“猜到”(我用finfo来表示MIME类型,但它在2013年有一个bug,这只会加剧这个问题),但即使是apache也不能100%正确地做到这一点...
然后删除图像的缓存控制头,否则你不仅可以经常PHP页面,以及...
我只想说;你只需要,如果你有厚厚的管道和很多你并不需要的CPU周期来做到这一点...
来源
2013-07-19 13:35:42
lol
最好的答案是:**不”做。 **开放访问css/js文件,保护php文件,使用'拒绝所有'来保护子目录。很多流行的WordPress博客都可以打开'/ wp_admin/*'([example](http://www.gizmodo.co.uk/wp-login.php?redirect_to=http%3A%2F%2Fwww.gizmodo)。 co.uk%2Fwp-admin%2F&reauth = 1)),只要在你的php文件中没有安全问题,它确实没关系 – Peter
为什么当未经授权的人会看到任何图像,样式表或JavaScript文件?其中是否有敏感数据? – Gumbo
@彼得Szymkowski谢谢你的答案。并且感谢你使用'deny from all'的想法,如果有必要的话,我认为它可以和'allow from IP'一起使用。 – acoder