Using “auto_prepend_file” into “.user.ini” file in PHP

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 08:23:51

问题


I want to load the file variables.php (that simply contains variables) at the run of any pages of my project so I can use the variables from any place.

I created a file called .user.ini (and placed it in the root folder):

; Automatically add files before PHP document.
; http://php.net/auto-prepend-file
auto_prepend_file = /Volumes/www/project_name/admin/libs/variables.php

It doesn't work. It seems that PHP doesn't read the .user.ini file.

php.ini is right configured by default:

user_ini.cache_ttl    300           300
user_ini.filename     .user.ini     .user.ini

Where am I wrong?


回答1:


Well, the manual says it all:

Since PHP 5.3.0, PHP includes support for configuration INI files on a per-directory basis. These files are processed only by the CGI/FastCGI SAPI. This functionality obsoletes the PECL htscanner extension.

And:

If you are using Apache, use .htaccess files for the same effect.

... though it actually refers to the Apache module.

If you need SAPI-independent custom settings I'm afraid you'll have to use both. You can minimise the maintenance burden if you keep those settings to the minimum (most PHP directives can be provided in PHP code itself). And you need to ensure that .htaccess settings don't crash when mod_php is not available:

<IfModule mod_php5.c>
    php_value auto_prepend_file /Volumes/www/project_name/admin/libs/variables.php
</IfModule>



回答2:


I think .user.ini should be located in project root folder, for example, /Volumes/www/project_name/.user.ini

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.

.user.ini documentation



来源:https://stackoverflow.com/questions/24778706/using-auto-prepend-file-into-user-ini-file-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!