簡體   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