Apache:在继续原始文件之前,使用PHP脚本作为安全检查的处理程序


Apache: Use a PHP script as a handler for security screening before continuing to original file

我可以使用php脚本作为加载目录中的文档的处理程序吗?

我的。htaccess (in "/path/to/")会显示:

AddHandler handler .png
Action handler /path/to/security.php

security.php会做它应该做的事情,例如检查数据库等,然后继续加载原始文件。例如:

用户试图加载'/path/to/awesome_picture.png'。服务器检查.htaccess并查看处理程序。它执行处理程序。在此之后,PHP脚本将重定向到原始文件,如果它认为合适的话。因此,用户最终会收到图片,但在此过程中会接受检查。

这是可能的吗?如果是,我该怎么做?

Thanks in advance

查看Apache文档,似乎可以,因为第二个示例与您编写的配置匹配。

但使用mod_rewrite似乎是一个更好的主意(至少对我来说),像这样:

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} ^(.*'.png)$
RewriteRule ^(.*)$ /path/to/security.php?img=$1 [NC,L]

然后您可以通过$_GET['img']在PHP脚本中引用请求的文件。