Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : PHP Options&Information : ini_get

ini_get

Gets the value of a configuration option (PHP 4, PHP 5)
string ini_get ( string varname )

Example 1845. A few ini_get() examples

<?php
/*
Our php.ini contains the following settings:

display_errors = On
register_globals = Off
post_max_size = 8M
*/

echo 'display_errors = ' . ini_get('display_errors') . "\n";
echo
'register_globals = ' . ini_get('register_globals') . "\n";
echo
'post_max_size = ' . ini_get('post_max_size') . "\n";
echo
'post_max_size+1 = ' . (ini_get('post_max_size')+1) . "\n";
echo
'post_max_size in bytes = ' . return_bytes(ini_get('post_max_size'));

function
return_bytes($val) {
   
$val = trim($val);
   
$last = strtolower($val{strlen($val)-1});
   switch(
$last) {
       
// The 'G' modifier is available since PHP 5.1.0
       
case 'g':
           
$val *= 1024;
       case
'm':
           
$val *= 1024;
       case
'k':
           
$val *= 1024;
   }

   return
$val;
}

?>

The above example will output something similar to:

display_errors = 1
register_globals = 0
post_max_size = 8M
post_max_size+1 = 9
post_max_size in bytes = 8388608

Code Examples / Notes » ini_get

david dot tulloh

You can set custom entries in the ini file to provide globals such as database details.
However these must be retrieved with get_cfg_var, ini_get won't work.


donations

true, I'd use
<?php
$target="FUNC"; //INI_GET Target Function
if(!function_exists('ini_get'))
{
ini_get($target) or exit("Cannot collect ini_get() function");
}
?>


khkjr

This freakin conversion to bytes function does not work, nor does this one: ben at qolc dot net 22-Feb-2007 04:20

marcus

The last comment about setting values in .htaccess is not right. These lines both result in display_errors being turned off:
php_value display_errors true
php_value display_errors off
So PHP does NOT coerce the value into a boolean, but it checks for exact values of the string. These both work:
php_value display_errors on
php_value display_errors 1


ben

Re Webmaster at Grimchild's code
As petrov says, grimchild's code doesn't "parse" the value, it just throws away the most significant part of it. Out of there you'll get a number but won't know whether it's bytes, kilobytes, megabytes or gigabytes!
To parse the value you have to split off the character (if present) and multiply the number appropriately. Trivially:
<?php
$pms = ini_get('post_max_size');
if (preg_match('/^([\d\.]+)([gmk])?$/i', $pms, $m)) {
 $value = $m[1];
 if (isset($m[2])) {
   switch(strtolower($m[2])) {
     case 'g': $value *= 1024;  # fallthrough
     case 'm': $value *= 1024;  # fallthrough
     case 'k': $value *= 1024; break;
     default: die("Can't parse value '$pms' ");
   }
 }
 print "$value bytes\n";
} else {
 die("Can't parse value '$pms' ");
}
?>
Note the cunning or horrible (depending on your viewpoint) use of fallthrough between cases in the switch statement, so that for example 'g' gets multiplied by 1024 three times. If you ever write code which intentionally uses switch fallthrough like the above, be a good boy/girl and comment it, else someday it will come and bite you or your unfortunate cow-orkers.
It wouldn't surprise me if there's a PHP function to parse human-format sizes like this already (given that PHP itself has to do it).


webmaster

Just thought I would add.
Very simple way of parsing data such as post_max_size
eregi_replace('[gmk]','',ini_get('post_max_size'));


der graph

It might be useful for included scripts that include other files to extend the 'include_path' variable:
<?  ini_set('include_path',ini_get('include_path').':../includes:');  ?>
Sometimes, it may also be useful to store the current 'include_path' in a variable, overwrite it, include, and then restore the old 'include_path'.


01-nov-2005 07:16

Important: The manual says that ini_get will return 0 or an empty string for boolean config values that are set to off in php.ini.
This is technically correct, however when you use
php_value register_globals off
in an .htaccess file, ini_get will return the string, which will "evaluate" to 1. So if you are using mod_php you have to check boolean config values against the strings (upper/lowercase etc.) anyhow or you will get wrong results.


fbeyer

If you want to test ini flags (eg. On/Off), I recommend to explicitly cast the value returned by ini_get() to boolean - it is cleaner as you only get true or false, not 0 or 1 or "" as described above.
<?php
   $register_globals = (bool) ini_get('register_gobals');
?>
C fans may of course also cast it to (int) to play with 0 and 1 - that's also cleaner to print().


filh

Concerning the value retourned, it depends on how you set it.
I had the problem with horde-3 which test the safe_mode value.
THan :
- if you set the value with php_admin_value safe_mode Off (or On) ini_get returns the string
- if you set the value with php_admin_flag safe_mode Off (or On) ini_get returns the boolean.


petrov dot michael

@Webmaster at GrimChild dot com
Your suggestion to simply strip the [gmk] letters would actually be very unpredictable since you no longer know the magnitude of the number. Therefore if you have 8K and 8M they would be both equal after your function. Also the following would be incorrect:
<?php
// Incorrect code
function parse_size($size) {
   return eregi_replace('[gmk]','',$size);
}
//Check that we have at least 4M of post space
//      assume post_max_size = 16K
if(parse_size(ini_get('post_max_size')) < parse_size("4M")) {
     die('Error: this script requires more than 4M of post space');
}
?>
As you can see in this scenario the functions would return 16 and 4 respectively, allowing the check to succeed with less memory than 4M.


09-nov-2005 09:56

@marcus at synchromedia dot co dot uk
you misunderstood the comment. Of course you can use on and off, and they will work correctly.
But this entry is about *ini_get*. And if you ini_get() a value that is set to "off" in php.ini, you get returned 0 (as described above in the documentation). If you ini_get() a value that is set to "off" via .htaccess, you will be returned the string "off", which - if you use it in an if-clause, will be autoconverted to 1 (as is usual for strings).
So the problem is that which is returned by ini_get(), not what you can and cannot use in .htaccess. Sorry being unclear about this.


Change Language


Follow Navioo On Twitter
assert_options
assert
dl
extension_loaded
get_cfg_var
get_current_user
get_defined_constants
get_extension_funcs
get_include_path
get_included_files
get_loaded_extensions
get_magic_quotes_gpc
get_magic_quotes_runtime
get_required_files
getenv
getlastmod
getmygid
getmyinode
getmypid
getmyuid
getopt
getrusage
ini_alter
ini_get_all
ini_get
ini_restore
ini_set
main
memory_get_peak_usage
memory_get_usage
php_ini_scanned_files
php_logo_guid
php_sapi_name
php_uname
phpcredits
phpinfo
phpversion
putenv
restore_include_path
set_include_path
set_magic_quotes_runtime
set_time_limit
sys_get_temp_dir
version_compare
zend_logo_guid
zend_thread_id
zend_version
eXTReMe Tracker