【问题标题】:Deny access to .php files apart from the server itself拒绝访问除服务器本身之外的 .php 文件
【发布时间】:2013-12-21 23:46:10
【问题描述】:

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

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

【问题讨论】:

  • 把它们放在外面DOCUMENT_ROOT
  • 你的标题有点二分法。请求他们的用户是服务器

标签: php .htaccess


【解决方案1】:

这可以解决问题

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

另外,请参阅此问题的答案:Deny direct access to all .php files except index.php

【讨论】:

  • 感谢 Dmitry,已修复。
  • 抱歉我之前的评论,我弄错了——可以在 .htaccess 中使用 File 指令。我刚刚查看了 Apache 文档并检查了 变体 - 它工作得很好。
【解决方案2】:

我通常会这样做:

如果我希望用户访问文件,请说 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' );
     ....
?>

其中page_is_authorized($s) 会根据有效值列表检查$s

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

【讨论】:

  • +1 关于可移植性。如果由于某种原因在服务器上禁止使用 .htaccess,这会有所帮助。
猜你喜欢
  • 2012-10-26
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多