繁体   English   中英

拒绝直接访问除index.php之外的所有.php文件

[英]Deny direct access to all .php files except index.php

我想拒绝直接访问所有.php文件,除了一个: index.php

对其他.php文件的唯一访问应该是通过php include

如果可能,我希望所有文件都在同一个文件夹中。

更新:

一般规则会很好,所以我不需要浏览所有文件。 风险是我忘记了文件或行。

更新2:

index.php位于www.myadress.com/myfolder/index.php文件夹中

我想拒绝访问myfolder和子文件夹中的所有.php文件到该文件夹​​。

你确定,你想这样做吗? 甚至css和js文件和图像......?

好的,首先检查是否已将mod_access安装到apache中,然后将以下内容添加到.htaccess:

Order Deny,Allow
Deny from all
Allow from 127.0.0.1

<Files /index.php>
    Order Allow,Deny
    Allow from all
</Files>

第一个指令禁止访问除localhost之外的任何文件,因为Order Deny,Allow ,Allow稍后应用,第二个指令只影响index.php。

警告:订单行中逗号后面没有空格。

要允许访问与* .css或* .js匹配的文件,请使用以下指令:

<FilesMatch ".*\.(css|js)$">
    Order Allow,Deny
    Allow from all
</FilesMatch>

但是,您不能在.htaccess文件中使用<Location><Directory>指令。

您的选择是在第一个允许,拒绝组周围使用<FilesMatch ".*\\.php$"> ,然后明确允许访问index.php。

Apache 2.4的更新:这个答案适用于Apache 2.2。 在Apache 2.4中, 访问控制范例已经改变,正确的语法是使用Require all denied

实际上,我带着与该主题的创建者相同的问题来到这里,但所给出的解决方案都不是我问题的完整答案。 为什么只需配置一次代码就可以为服务器上的所有文件添加代码? 最接近的是Residuum的一个,但是,当我想排除那些未命名为index.php的php文件时,他仍然排除了所有文件。

所以我想出了一个包含这个的.htaccess:

<Files *.php>
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
</Files>

<Files index.php>
    Order Allow,Deny
    Allow from all
</Files>

(请记住,htaccess文件是递归工作的,所以它完全适合问题的先决条件。)

现在我们开始。 用户可以访问的唯一php文件将是名为index.php的文件。 但您仍然可以访问每个图像,css样式表,js脚本等。

这个问题的一个倾斜的答案是将所有代码写为类,除了index.php文件,这是唯一的入口点。 包含类的PHP文件不会导致任何事情发生,即使它们是通过Apache直接调用的。

直接的答案是在.htaccess中包含以下内容:

<FilesMatch "\.php$">
    Order Allow,Deny
    Deny from all
</FilesMatch>
<FilesMatch "index[0-9]?\.php$">
    Order Allow,Deny
    Allow from all
</FilesMatch>

这将允许访问任何文件,如index.php,index2.php等,但将拒绝任何类型的访问其他.php文件。 它不会影响其他文件类型。

你可以尝试在index.php定义一个常量并添加类似的东西

if (!defined("YOUR_CONSTANT")) die('No direct access');

到其他文件的开头。

或者,您可以使用mod_rewrite和重定向请求到index.php,编辑.htaccess如下所示:

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L,R=301]

然后,您应该能够分析index.php中的所有传入请求并采取相应的操作。

例如,如果你想省略所有* .jpg,* .gif,* .css和* .png文件,那么你应该像这样编辑第二行:

RewriteCond $1 !^(index\.php|*\.jpg|*\.gif|*\.css|*\.png)

除了在web根目录之上的index.php之外,保留所有.php文件怎么样? 无需任何重写规则或程序化的kludges。

将includes-folder添加到include路径将有助于简化操作,无需使用绝对路径等。

URL重写可用于将URL映射到.php文件。 以下规则可以识别.php请求是直接发出还是重写。 它在第一种情况下禁止请求:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^.+?\ [^?]+\.php[?\ ]
RewriteRule \.php$ - [F]
RewriteRule test index.php

这些规则将禁止以.php结尾的所有请求。 但是,仍然允许使用/ (触发index.php), /test (重写为index.php)和/test?f=index.php (其中包含querystring中的index.php)等/test?f=index.php

THE_REQUEST包含浏览器发送到服务器的完整HTTP请求行(例如, GET /index.php?foo=bar HTTP/1.1

我没有传递一个变量,而是在你不想直接访问的任何页面的顶部自行包含这个变量,这应该仍然与.htaccess规则配对,但我知道如果有一个故障安全,我会感到更安全。 htaccess永远搞砸了。

<?php
// Security check: Deny direct file access; must be loaded through index
if (count(get_included_files()) == 1) {
    header("Location: index.php"); // Send to index
    die("403"); // Must include to stop PHP from continuing
}
?>

使用Apache 2.4,访问控制的语法已经改变。 如果使用如下指令:

Order deny,allow
Deny from all

不起作用,你可能使用Apache 2.4。

Apache 2.4文档就在这里

您需要以下列方式之一使用Require all denied

# you can do it this way (with "not match")

<FilesMatch ^((?!index\.php).)*$>
  Require all denied
</FilesMatch>

# or 

Require all denied

<Files index.php>
  Require all granted
</Files>

一个简单的解决方案是将所有非index.php文件重命名为.inc,然后拒绝访问* .inc文件。 我在很多项目中都使用它,它的工作原理非常好。

将所有文件放在一个文件夹中。 将.htaccess文件放在该文件夹中,并为其赋予值deny all。 然后在index.php中放置在文件夹之外的文件让它根据用户输入或事件回显出正确的页面

<Files ~ "^.*\.([Pp][Hh][Pp])">
 Order allow,deny
 Deny from all
 Satisfy All
</Files>

只允许2个ip,所有其他将阻止

Order Deny,Allow
Deny from all
Allow from 173.11.227.73 108.222.245.179

在index.php中,添加如下访问值:

$access = 'my_value';

在每个其他文件中,包括此检查甚至在php回显单个字节之前:

if(empty($access)) {
    header("location:index.php"); 
    die();
}

这样,其他php文件只能通过require / include而不是url访问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM