我可以在 PHP 的文档根目录外放一个 .user.ini 文件吗?
Can I put a .user.ini file outside the document root in PHP?
我有一个我创建的 PHP 网络应用程序,组织在 src、vendor 和 web 等文件夹中,其中 'web' 被设置为我服务器上的文档根目录。这样做的原因是为了防止访问不在 'web'.
中的任何其他文件
我正在使用 .user.ini 文件来简单地修改一些全局设置,但是当我将它放在文档根目录(Web 的父级)的上一级时,配置集根本不会被触发。当我把它放在 'web' 里面时,一切正常。
这是什么原因?
有什么方法可以让 .user.ini 像我建议的那样工作吗?
出于安全考虑,我更愿意这样做,而不是拒绝在 web 中使用 .htaccess 进行访问。
我正在使用 Fast CGI 作为我的 PHP 处理程序并且不想更改它。
简短的回答,似乎你不能将文件放在指定目录之外。
根据http://php.net/manual/en/configuration.file.per-user.php,
In addition to the main php.ini file, PHP scans for INI files in each
directory, starting with the directory of the requested PHP file, and
working its way up to the current document root (as set in
$_SERVER['DOCUMENT_ROOT']). In case the PHP file is outside the
document root, only its directory is scanned.
网络服务器不应提供点文件
但是,您的网络服务器根本不应提供 /^./ 文件。如果您的 Web 服务器阻止访问以点开头的文件,那么您就没有泄露该文件的风险。
在代码中设置配置
另一种选择是在您的源代码中而不是在 .user.ini 文件中设置 ini 设置。
http://php.net/manual/en/ini.list.php 列出了您可以通过 ini_set()
设置的所有 ini 设置。特别是标记为 ** PHP_INI_USER**.
的选项
我为我的数据库用户名、密码和数据库名称执行此操作。我像这样包含 ini 文件:
$parsed_ini_variables_array = parse_ini_file('/home/username/ini_folder/file_name.ini');
然后,在 ini 文件中:
;Comment
[section name]
key=value
key2=value2
希望对您有所帮助。
我有一个我创建的 PHP 网络应用程序,组织在 src、vendor 和 web 等文件夹中,其中 'web' 被设置为我服务器上的文档根目录。这样做的原因是为了防止访问不在 'web'.
中的任何其他文件我正在使用 .user.ini 文件来简单地修改一些全局设置,但是当我将它放在文档根目录(Web 的父级)的上一级时,配置集根本不会被触发。当我把它放在 'web' 里面时,一切正常。
这是什么原因? 有什么方法可以让 .user.ini 像我建议的那样工作吗?
出于安全考虑,我更愿意这样做,而不是拒绝在 web 中使用 .htaccess 进行访问。
我正在使用 Fast CGI 作为我的 PHP 处理程序并且不想更改它。
简短的回答,似乎你不能将文件放在指定目录之外。
根据http://php.net/manual/en/configuration.file.per-user.php,
In addition to the main php.ini file, PHP scans for INI files in each directory, starting with the directory of the requested PHP file, and working its way up to the current document root (as set in $_SERVER['DOCUMENT_ROOT']). In case the PHP file is outside the document root, only its directory is scanned.
网络服务器不应提供点文件
但是,您的网络服务器根本不应提供 /^./ 文件。如果您的 Web 服务器阻止访问以点开头的文件,那么您就没有泄露该文件的风险。
在代码中设置配置
另一种选择是在您的源代码中而不是在 .user.ini 文件中设置 ini 设置。
http://php.net/manual/en/ini.list.php 列出了您可以通过 ini_set()
设置的所有 ini 设置。特别是标记为 ** PHP_INI_USER**.
我为我的数据库用户名、密码和数据库名称执行此操作。我像这样包含 ini 文件:
$parsed_ini_variables_array = parse_ini_file('/home/username/ini_folder/file_name.ini');
然后,在 ini 文件中:
;Comment
[section name]
key=value
key2=value2
希望对您有所帮助。