Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

ini

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?

like image 936
Fred K Avatar asked Feb 22 '26 15:02

Fred K


2 Answers

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>
like image 73
Álvaro González Avatar answered Feb 24 '26 03:02

Álvaro González


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

like image 33
Stanislav Shabalin Avatar answered Feb 24 '26 03:02

Stanislav Shabalin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!