保护php文件不被直接访问

lma*_*occ 4 php .htaccess

这就是我网站的地图:

root:
     -index.php
     -\actions\ (various php files inside and a .htaccess)
     - \includes\ (various php files inside and a .htaccess)
     - .htaccess
Run Code Online (Sandbox Code Playgroud)

我知道如果我在动作中使用"拒绝所有人"并包含目录,那么其中的文件将受到直接访问的保护.

现在,我的actions文件夹有许多由index.php(表单)调用的php文件.我在.htaccess里面"拒绝所有人" \actions,但后来我禁止访问那些文件.

那么,我如何保护文件免受直接url访问,但有可以从中调用的异常index.php

Mar*_*sch 6

最简单的方法是将一个恒定的index.php文件,并在其他PHP文件检查,如果这个常数存在.如果没有,让脚本死掉.

的index.php

define('APP', true);
Run Code Online (Sandbox Code Playgroud)

various.php

if(!defined('APP')) die();
Run Code Online (Sandbox Code Playgroud)

  • 一个更友好的替代`die()`:`header('Location:/index.php')` (2认同)