拒绝访问除服务器本身之外的.php文件


Deny access to .php files apart from the server itself

如何编写.htaccess脚本来阻止用户在某个目录及其子目录中运行.php文件?

不过,我需要能够包括服务器index.php或其他文件中的php文件,基本上我想让服务器包括某个目录中的php文件,但如果用户输入特定的URL,它会拒绝访问。

这可以实现

<FilesMatch "'.php$">
   Deny from all
<FilesMatch>

另外,请参阅此问题的答案:拒绝直接访问除index.php 之外的所有.php文件

我通常会做这样的事情:

如果我想让用户访问一个文件,比如members.phpindex.php

<?php
    $GLOBAL_PAGE="page name"; //set the 'visible' flag
    include_once 'headers.php'
...
?>

headers.php:中

<?php
     if(!page_is_authorized($GLOBAL_PAGE))
         //redirect to a valid page
         header( 'Location: index.php' );
     ....
?>

其中CCD_ 4对照有效值的列表来检查CCD_。

我知道这并没有按照你的要求使用.htaccess规则,但我通常没有访问该文件的权限,所以就这样。它也是便携的!