parse_ini_file
parse_ini_file
Parse a configuration file
**parse_ini_file** string $filename bool $process_sections int $scanner_mode
loads in the
ini file specified in ,
and returns the settings in it in an associative array.
parse_ini_file``filename
The structure of the ini file is the same as the php.ini's.
Waarschuwing: > This function must not be used with untrusted inputs, unless is since the parsed output might contain the values of sensitive constants, such as constants holding a database password.
scanner_mode``INI_SCANNER_RAW
filename
The filename of the ini file being parsed. If a relative path is used,
it is evaluated relative to the current working directory, then the
.
include_path
process_sections
By setting the
parameter to true, you get a multidimensional array, with
the section names and settings included. The default
for is false
process_sections``process_sections
scanner_mode
Can either be (default) or
. If
is supplied, then option values will not be parsed.
INI_SCANNER_NORMAL``INI_SCANNER_RAW``INI_SCANNER_RAW
The settings are returned as an associative on success,
and false on failure.
array
**Voorbeeld: Contents of **
; This is a sample configuration file
; Comments start with ';', as in php.ini
[first_section]
one = 1
five = 5
animal = BIRD
[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"
[third_section]
phpversion[] = "5.0"
phpversion[] = "5.1"
phpversion[] = "5.2"
phpversion[] = "5.3"
urls[svn] = "http://svn.php.net"
urls[git] = "http://git.php.net"
Voorbeeld: example
(but not "magic constants" like )
may also be parsed
in the ini file so if you define a constant as an ini value before
running , it will be integrated into
the results. Only ini values are evaluated, and the value must be just the constant. For example:
Constants`__FILE__``parse_ini_file`
<?php
define('BIRD', 'Dodo bird');
// Parse without sections
$ini_array = parse_ini_file("sample.ini");
print_r($ini_array);
// Parse with sections
$ini_array = parse_ini_file("sample.ini", true);
print_r($ini_array);
?>
Array
(
[one] => 1
[five] => 5
[animal] => Dodo bird
[path] => /usr/local/bin
[URL] => http://www.example.com/~username
[phpversion] => Array
(
[0] => 5.0
[1] => 5.1
[2] => 5.2
[3] => 5.3
)
[urls] => Array
(
[svn] => http://svn.php.net
[git] => http://git.php.net
)
)
Array
(
[first_section] => Array
(
[one] => 1
[five] => 5
[animal] => Dodo bird
)
[second_section] => Array
(
[path] => /usr/local/bin
[URL] => http://www.example.com/~username
)
[third_section] => Array
(
[phpversion] => Array
(
[0] => 5.0
[1] => 5.1
[2] => 5.2
[3] => 5.3
)
[urls] => Array
(
[svn] => http://svn.php.net
[git] => http://git.php.net
)
)
)
Voorbeeld: parsing a php.ini file
<?php
// A simple function used for comparing the results below
function yesno($expression)
{
return($expression ? 'Yes' : 'No');
}
// Get the path to php.ini using the php_ini_loaded_file() function
$ini_path = php_ini_loaded_file();
// Parse php.ini
$ini = parse_ini_file($ini_path);
// Print and compare the values, note that using get_cfg_var()
// will give the same results for parsed and loaded here
echo '(parsed) magic_quotes_gpc = ' . yesno($ini['magic_quotes_gpc']) . PHP_EOL;
echo '(loaded) magic_quotes_gpc = ' . yesno(get_cfg_var('magic_quotes_gpc')) . PHP_EOL;
?>
(parsed) magic_quotes_gpc = Yes
(loaded) magic_quotes_gpc = Yes
Voorbeeld: Value Interpolation
In addition to evaluating constants, certain characters have special meaning in an ini value.
Additionally, environment variables and previously defined configuration options (see ) may be read using
syntax.
`get_cfg_var``${}`
; | is used for bitwise OR
three = 2|3
; & is used for bitwise AND
four = 6&5
; ^ is used for bitwise XOR
five = 3^6
; ~ is used for bitwise negate
negative_two = ~1
; () is used for grouping
seven = (8|7)&(6|5)
; Interpolate the PATH environment variable
path = ${PATH}
; Interpolate the configuration option 'memory_limit'
configured_memory_limit = ${memory_limit}
Voorbeeld: Escaping Characters
Some characters have special meaning in double-quoted strings and must be escaped by the backslash prefix.
First of all, these are the double quote as the boundary marker, and the backslash itself
(if followed by one of the special characters):
`"``\`
quoted = "She said \"Exactly my point\"." ; Results in a string with quote marks in it.
hint = "Use \\\" to escape double quote" ; Results in: Use \" to escape double quote
There is an exception made for Windows-like paths: it's possible to not escape trailing backslash if the quoted string is directly followed by a linebreak:
save_path = "C:\Temp\"
If one does need to escape double quote followed by linebreak in a multiline value, it's possible to use value concatenation in the following way (there is one double-quoted string directly followed by another one):
long_text = "Lorem \"ipsum\"""
dolor" ; Results in: Lorem "ipsum"\n dolor
Another character with special meaning is (the dollar sign).
It must be escaped if followed by the open curly brace:
`$`
code = "\${test}"
Escaping characters is not supported in the mode
(in this mode all characters are processed "as is").
`INI_SCANNER_RAW`
Note that the ini parser doesn't support standard escape sequences (, , etc.).
If necessary, post-process the result of with function.
`\n``\t``parse_ini_file``stripcslashes`
Opmerking: > This function has nothing to do with the php.ini file. It is already processed by the time you run your script. This function can be used to read in your own application's configuration files.
Opmerking: > If a value in the ini file contains any non-alphanumeric characters it needs to be enclosed in double-quotes (").
Opmerking: > There are reserved words which must not be used as keys for ini files. These include: , , , , , , , . Values , , and result in , and values , and result in , unless mode is used. Characters must not be used anywhere in the key and have a special meaning in the value.
null``yes``no``true``false``on``off``none``null``off``no``false``""``on``yes``true``"1"``INI_SCANNER_TYPED``?{}|&~!()^"
Opmerking: > Entries without an equal sign are ignored. For example, "foo" is ignored whereas "bar =" is parsed and added with an empty value. For example, MySQL has a "no-auto-rehash" setting in that does not take a value, so it is ignored.
Opmerking: > ini files are generally treated as plain text by web servers and thus served to browsers if requested. That means for security you must either keep your ini files outside of your docroot or reconfigure your web server to not serve them. Failure to do either of those may introduce a security risk.
parse_ini_string