[英]All files download only through index.php and deny direct access to them from browser
[英]Restrict direct access to .php files through browser and allow only index.php to access them
我的網站上有一個腳本,允許用戶編輯/創建他們的帳戶。 我想限制通過瀏覽器直接訪問這些文件(例如 www.mydomain.com/cp/page/login.php 或 www.mydomain.com/cp/home.php)並只允許 index.php 訪問這些文件. 我嘗試使用 .htaccess 但 index.php 無法訪問它們。 我也無法將它們移出 public_html 文件夾。 它不包括文件夾。 我怎么能做到這一點? 如果你需要別的東西,現在請讓我。
一種方法是使用 PHP 中的include
或require
調用:
include '/path/to/script.php';
include
在服務器端由 PHP 處理,因此 Apache 塊不會對此產生影響。
然后你可以保留現有的<Files>
指令來阻止對.php
文件的訪問:
<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>
PHP 中的一種常見方法是在index.php
定義一些內容並在其他內容中檢查它:
//index.php
define('INDEX', true);
if(isset($_GET['page'])) {
if($_GET['page'] == 'home') {
include('cp/home.php');
}
}
//home.php
if(!defined('INDEX') { die(); }
//more code
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.