The “.user.ini” File in PHP and an Example
PHP versions 5.3 and above include a feature which is really handy, especially if you don’t use Apache as your web server (or any web server that uses .htaccess files). The .user.ini files can be used on a per-directory basis with web servers that use CGI or FastCGI (like PHP-FPM) to process PHP scripts.
If your web server uses .htaccess files, you can do the same things with them, so it’s not like you’re limited if you can’t use .user.ini files with PHP.
PHP “.user.ini” Files
If you’re familiar with Apache as a web server, you should already be familiar with .htaccess files. While there are other web servers that use them, there are some that don’t and never will. Nginx, the web server I use, is one of them. PHP versions 5.3 and above support a slightly different type of file which is used in much the same way. In other words, PHP picks up where Nginx leaves off.
You can change the name of the .user.ini file in php.ini, but it really isn’t necessary. If you have a directory structure like this, a single .user.ini file at the top will be inherited by the directories below it:
This means you only have to set the file up once per web site if you don’t need to do a lot with it. Of course, you should probably be aware that it only affects the PHP functions that are controlled on a “per directory” basis and if you want to ignore it in lower directories, you’ll have to create files with directives that override the higher level file’s directives.
I’ll give you a real-world example so you can understand where I’m coming from.
Auto Prepend File
I won’t get into why using this particular feature was rarely used before PHP version 5.3. In my opinion, to use it effectively was more trouble than it was worth.
In the past, I included a configuration file at the beginning of every page. The reason I didn’t use an auto prepend file is because I didn’t want to change other websites on the same server to accommodate a “global” auto prepend file. With the .user.ini option, I can put something like auto_prepend_file = ‘/home/config.php’ in the file and let the file live at /home/www/. It’s pretty safe there because I have restrictions in place to prevent files starting with “.” from being read by anything other than the web server or PHP. To disable it in lower directories, I can put autoprepend file = “” in the .user.ini files in those directories.
In essence, the .user.ini file makes it easy to have a unique auto prepend file for every web site or even every directory. By the way, you can do the same thing with Apache by putting php_value auto_prepend_file “path-to-filename” in the website root directory’s .htaccess file.
You can read more about the .user.ini file here, at the website for PHP itself: .user.ini files