限制通过浏览器直接访问.php文件,并仅允许index.php访问它们

Kos*_*lis 1 php .htaccess

我的网站上有一个脚本,允许用户编辑/创建他们的帐户。我想限制通过浏览器(例如www.mydomain.com/cp/page/login.php或www.mydomain.com/cp/home.php)直接访问文件,并且仅允许index.php访问这些文件。我尝试使用.htaccess,但index.php无法访问它们。我也不能将它们移出public_html文件夹。它不包括文件夹。我怎样才能做到这一点?如果您还有其他需要,请让我现在。

anu*_*ava 5

一种实现方法是使用PHP includerequire从PHP调用:

include '/path/to/script.php';
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)