将 PHP 文件嵌入 INI

Embed a PHP file into INI

我想在 php.ini 中插入一行,类似于 include('path to php file.php')。 其实我已经从网上找到了这个解决方案,但是我不记得在这种情况下php.ini中使用了哪个参数。

为什么:我想在来自我的网络服务器的每个 php 脚本之前执行一段短代码(由 php 编写)。

如果我没理解错的话,您是在寻找 运行 一个 PHP 脚本在服务器上的所有文件之前?如果是这样,auto-prepend-file directive in php.ini configuration 是完美的选择。来自帮助文档:

Specifies the name of a file that is automatically parsed before the main file. The file is included as if it was called with the require function, so include_path is used.

The special value none disables auto-prepending.

示例代码:

# Inside either main php.ini or child file
auto_prepend_file=/path/to/your/global/php/file.php 

请注意,PHP 还提供了在每个脚本解释后 追加 文件的能力。还有一个带有附加信息的 previous SO entry。 HTH.

您必须搜索 auto_prepend_file or auto_append_file,即:

auto_prepend_file 字符串

Specifies the name of a file that is automatically parsed before the main file. The file is included as if it was called with the require function, so include_path is used.

auto_append_file 字符串

Specifies the name of a file that is automatically parsed after the main file. The file is included as if it was called with the require function, so include_path is used.


用法php.ini

auto_prepend_file="/path/to/prepend.php"
auto_append_file="/path/to/append.php"