Fred K
Fred K

Reputation: 13910

Using "auto_prepend_file" into ".user.ini" file in PHP

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?

Upvotes: 3

Views: 19888

Answers (2)

Álvaro González
Álvaro González

Reputation: 146540

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 (mod_php).

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>

Upvotes: 7

Stanislav Shabalin
Stanislav Shabalin

Reputation: 19278

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

Upvotes: 1

Related Questions