How to Include a PHP.Ini File in Another PHP.Ini File

How do I include a php.ini file in another php.ini file?

I don't think you can "include" .ini files from the main php.ini file.

One possible solution, though, might be to use this option on the configure line, when compiling PHP:

--with-config-file-scan-dir=PATH
Set the path where to scan for configuration files

If this option is used at compile-time, PHP will look for every .ini file in this directory, in addition to the "normal" php.ini file.

I suppose this is what is used by Ubuntu, for instance, which uses a different .ini file for each downloaded extension, instead of modifying php.ini.

The path to the php.ini file is being defined with this option, on the configure line:

--with-config-file-path=PATH
Set the path in which to look for php.ini [PREFIX/lib]

Still, it probably means you'll have to re-compile PHP -- which is not that hard, btw -- the hardest part being to get the dependencies you need.

And, here is a post on the internals@ mailling-list that says the same thing as I do: config files and PHP_CONFIG_FILE_SCAN_DIR

Is possible to split PHP configuration file php.ini?

If your PHP installation is setup to scan for .ini files you can drop several of them in a folder. If you installed PHP through the Ubuntu repos, it should already be configured this way.

My phpinfo() (note: The additional ini files are the result of installing php extensions from the repos and won't be included with PHP):

phpinfo with ini sections highlighted

The setting for this directory is a compile-time option:

--with-config-file-scan-dir=/etc/php5/apache2/conf.d

You can also set an environment variable through Apache:

SetEnv PHP_INI_SCAN_DIR /php/custom/scan/directory

More info @ ServerFault

PHP use another php.ini file then set

You have to restart the apache server after changing php.ini so that its changes reflects.

Reference Doc

How to change the path for additional php.ini files

That directory is defined when PHP is compiled from source code using the custom flag --with-config-file-scan-dir:
https://www.php.net/manual/en/configure.about.php#configure.options.php

You can change it at run time by setting an ENV var called PHP_INI_SCAN_DIR as defined in the documentation:
https://www.php.net/manual/en/configuration.file.php#configuration.file.scan

Embed a PHP file into INI

If I understand you correctly, you're looking to run a PHP script before all files on your server? If so, the auto-prepend-file directive in php.ini configuration is perfect for this. From the help docs:

Specifies the name of a file that is automatically parsed before the
main file. The file is included as if it was called with the require
function, so include_path is used.

The special value none disables auto-prepending.

Sample Code:

# Inside either main php.ini or child file
auto_prepend_file=/path/to/your/global/php/file.php

Note that PHP also gives the ability to append a file after every script interpretation as well. There is also a previous SO entry for this with additional information. HTH.



Related Topics



Leave a reply



Submit