我可以使用.htaccess预先添加PHP文件吗?

tim*_*yRS 4 php .htaccess

所以我在阅读Twitter之前,直到我看到@DivineOmega的这条推文:

图

完美的PHP错误处理程序(差不多),我编写它并且我想在服务器范围内使用它,但是如何将此文件应用于我的所有PHP脚本?

max*_*xhb 6

您可以使用phps auto_prepend_fileauto_append_file指令.

它的工作方式就是在服务器上的每个脚本require_once()之间通过auto_prepend_file(在脚本之前加载)和auto_append_file(在脚本之后加载)指定的文件之间加载.

要激活.htaccess:

php_value auto_prepend_file "/path/to/file/before.php"
php_value auto_append_file "/path/to/file/after.php"
Run Code Online (Sandbox Code Playgroud)

或者php.ini(在cgi模式下运行时需要,影响wole webserver):

auto_prepend_file  = "/path/to/file/before.php"
auto_append_file   = "/path/to/file/after.php"
Run Code Online (Sandbox Code Playgroud)

before.php

try {
Run Code Online (Sandbox Code Playgroud)

after.php

} catch(Exception $e) {
  ...
}
Run Code Online (Sandbox Code Playgroud)