3

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?

Fred K
  • 13,249
  • 14
  • 78
  • 103
  • can you put the exact location of this file i.e. d:/.user.ini and see if it works – Satya Jul 16 '14 at 10:42
  • Does your PHP interpreter run as CGI, FastCGI or a similar SAPI? – Álvaro González Jul 16 '14 at 10:44
  • From the documentation: “The file is included as if it was called with the require function, so include_path is used.” Would you mind to put it inside the folder, accessible by the web server and give a relative path which is included (e.g. admin/libs/variables.php)? – Aleksei Matiushkin Jul 16 '14 at 10:50
  • @Satya: The above path is the exact location on my file system (mac osx) – Fred K Jul 16 '14 at 11:23
  • @mudasobwa: Tried to use relative path but doesn't work yet – Fred K Jul 16 '14 at 11:23
  • [Find out how PHP is running on server (CGI OR fastCGI OR mod_php)](http://stackoverflow.com/questions/16414054/find-out-how-php-is-running-on-server-cgi-or-fastcgi-or-mod-php) – Álvaro González Jul 16 '14 at 11:30
  • @ÁlvaroG.Vicario: thanks. It's "Apache 2.0 Handler" (`apache2handler`) on my local pc, but on the hosting I will have to work is `cgi-fcgi`. I need to get it work on both cases. – Fred K Jul 16 '14 at 11:37

2 Answers2

7

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>
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Good, on apache2handler it works (using htaccess) but on cgi I get this issue: the browser keep loading for a while, then it shows the page with a bunch of error about any php functions used in the page but I know it has loaded the variables.php file because a test echo. – Fred K Jul 16 '14 at 12:24
  • Well, if code is not right it doesn't matter how you load it :-? – Álvaro González Jul 16 '14 at 12:36
0

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

Stanislav Shabalin
  • 19,028
  • 3
  • 18
  • 18