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

时间:2009-08-27 09:48:30

标签: php .htaccess

我想拒绝直接访问除{1}}之外的所有.php个文件:index.php

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

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

更新

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

更新2:

index.php位于www.myadress.com/myfolder/index.php

文件夹中

我想拒绝访问.php中的所有myfolder个文件以及该文件夹的子文件夹。

13 个答案:

答案 0 :(得分:103)

你确定,你想这样做吗?甚至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,允许稍后应用,第二个指令只影响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中,access control paradigm已更改,正确的语法是使用Require all denied

答案 1 :(得分:80)

实际上,我带着与该主题的创建者相同的问题来到这里,但所给出的解决方案都不是我问题的完整答案。 为什么只需配置一次代码就可以为服务器上的所有文件添加代码? 最接近的是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脚本等。

答案 2 :(得分:34)

问题的一个倾向性答案是将所有代码写为类,除了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文件。它不会影响其他文件类型。

答案 3 :(得分:16)

您可以尝试在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)

答案 4 :(得分:11)

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

将include文件夹添加到include路径将有助于简化操作,无需使用绝对路径等。

答案 5 :(得分:5)

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)等URL。

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

答案 6 :(得分:2)

不是传递一个变量,而是在你不想直接访问的任何页面的顶部执行自包含,这应该仍然与.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
}
?>

答案 7 :(得分:1)

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

答案 8 :(得分:0)

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

答案 9 :(得分:0)

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

Order deny,allow
Deny from all

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

Apache 2.4 docs are here

您需要以下列方式之一使用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>

答案 10 :(得分:-1)

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

答案 11 :(得分:-2)

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

Order Deny,Allow
Deny from all
Allow from 173.11.227.73 108.222.245.179

答案 12 :(得分:-49)

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

$access = 'my_value';

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

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

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

相关问题