How can i check if ini_set will work on the server?

Polmonino picture Polmonino · Dec 17, 2011 · Viewed 8.7k times · Source

How can i check if server configuration allows me to set an option like:

ini_set('upload_max_filesize', '8M');

in a PHP script? Here is a list of php.ini directives but i can't really figure out how to make a check before tring to change that value.

Answer

Filip Roséen - refp picture Filip Roséen - refp · Dec 17, 2011

Check if I'm allowed to use ini_set for some option, how?

ini_set will return the old value on success, and false* on failure. With this knowledge you can write a statement checking if your call went through, like the below.

$result = ini_set ("some_option", "some_value");
$failure = (version_compare(PHP_VERSION, '5.3.0') >= 0) ? false : '';
if ($result === $failure)
   echo "Unable to use ini_set to change 'some_option'!";

(*): Note the return value changed in PHP 5.3.0 from '' (an empty string) to false. So you need to check your current PHP version as well.


Another method is to use ini_get_all which will provide you with details regarding every option available, and it's access level.

$all_option_details = ini_get_all ();

/* see the comments in this post regarding PHP_INI_USER vs INI_USER
 * seems like someone writing the relevant PHP documentation fcuked up
 *
 * props to @soulmerge */

if ($all_option_details['upload_max_filesize']['access'] & INI_USER)
   echo "we are allowed to change upload_max_filesize from with ini_set!";

I'd like to disable the use of ini_set for some option(s), how?

There are a few methods of making options unchangeable runtime (in a way disabling ini_set), among them are the following two which you can read more about at the provided link.

php_admin_value name value

Sets the value of the specified directive. This can not be used in .htaccess files. Any directive type set with php_admin_value can not be overridden by .htaccess or ini_set(). To clear a previously set value use none as the value.

 

php_admin_flag name on|off

Used to set a boolean configuration directive. This can not be used in .htaccess files. Any directive type set with php_admin_flag can not be overridden by .htaccess or ini_set().


Example (taken from this documentation)

<IfModule mod_php5.c>
  php_value include_path ".:/usr/local/lib/php"
  php_admin_flag engine on
</IfModule>

<IfModule mod_php4.c>
  php_value include_path ".:/usr/local/lib/php"
  php_admin_flag engine on
</IfModule>