|
użytkowników online: 42
|
OPINIE UŻYTKOWNIKÓW
|
Po wysłaniu do Dariusza problemu jeszcze nie opisanego w poradach, odpowiedź pojawia się na stronach już po 24 godzinach. To jedna z najważniejszych zalet serwisu! Za około 100 złotych rocznie mam profesjonalnego i doświadczonego konsultanta od technologii internetowych! Polecam serwis z poradami każdemu webmasterowi, niezależnie od stażu pracy i umiejętności.
Paweł Kowalski
grupa hiperMEDIA.pl
|
|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
parse_ini_file (PHP 4, PHP 5) parse_ini_file -- Parse a configuration file Descriptionarray parse_ini_file ( string filename [, bool process_sections] )
parse_ini_file() loads in the
ini file specified in filename,
and returns the settings in it in an associative array.
By setting the last process_sections
parameter to TRUE, you get a multidimensional array, with
the section names and settings included. The default
for process_sections is FALSE
Notatka:
This function has nothing to do with the
php.ini file. It is already processed,
the time you run your script. This function can be used to
read in your own application's configuration files.
Notatka:
If a value in the ini file contains any non-alphanumeric
characters it needs to be enclosed in double-quotes (").
Notatka:
As of PHP 5.0 this function also handles new lines in values.
Notatka:
There are reserved words which must not be used as keys for
ini files. These include: null, yes, no, true, and false.
The structure of the ini file is similar to that of
the php.ini's.
Constants may also be parsed
in the ini file so if you define a constant as an ini value before
running parse_ini_file(), it will be integrated into
the results. Only ini values are evaluated. For example:
Przykład 1. Contents of sample.ini ; 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" |
|
Przykład 2. parse_ini_file() example |
<?php
define('BIRD', 'Dodo bird');
$ini_array = parse_ini_file("sample.ini");
print_r($ini_array);
$ini_array = parse_ini_file("sample.ini", true);
print_r($ini_array);
?>
|
Would produce:
Array
(
[one] => 1
[five] => 5
[animal] => Dodo bird
[path] => /usr/local/bin
[URL] => http://www.example.com/~username
)
Array
(
[first_section] => Array
(
[one] => 1
[five] => 5
[animal] = Dodo bird
)
[second_section] => Array
(
[path] => /usr/local/bin
[URL] => http://www.example.com/~username
)
) |
|
Keys and section names consisting from numbers are evaluated as PHP
integers thus numbers
starting by 0 are evaluated as octals and numbers starting by 0x are
evaluated as hexadecimals.
User Contributed Notesahull at clydemarine dot com
26-Jan-2006 10:33
I had a look at the code for function parse_ini_file_quotes_safe(
and added in the ability to preserve comments.
<?php
function parse_ini_file_quotes_safe($f)
{
$newline = "<br>";
$null = "";
$r=$null;
$first_char = "";
$sec=$null;
$comment_chars="/*<;#?>";
$num_comments = "0";
$header_section = "";
$f=@file($f);
for ($i=0;$i<@count($f);$i++)
{
$newsec=0;
$w=@trim($f[$i]);
$first_char = @substr($w,0,1);
if ($w)
{
if ((!$r) or ($sec))
{
if ((@substr($w,0,1)=="[") and (@substr($w,-1,1))=="]") {$sec=@substr($w,1,@strlen($w)-2);$newsec=1;}
if ((stristr($comment_chars, $first_char) === FALSE)) {} else {$sec=$w;$k="Comment".$num_comments;$num_comments = $num_comments +1;$v=$w;$newsec=1;$r[$k]=$v;echo "comment".$w.$newline;}
}
if (!$newsec)
{
$w=@explode("=",$w);$k=@trim($w[0]);unset($w[0]); $v=@trim(@implode("=",$w));
if ((@substr($v,0,1)=="\"") and (@substr($v,-1,1)=="\"")) {$v=@substr($v,1,@strlen($v)-2);}
if ($sec) {$r[$sec][$k]=$v;} else {$r[$k]=$v;}
}
}
}
return $r;
}
?>
dewi at morganalley dot net
21-Oct-2005 08:45
[A feature request for a third parameter, to turn off the following insecure behaviour has been submitted: http://bugs.php.net/bug.php?id=34949 - I'm just documenting it here so that people are aware that they need to take the insecurity of the current behaviour into consideration when programming.]
Be warned that, in its current (2-argument) form, this function should be avoided when processing user-provided ini files, as data leakage may occur if the user provides an ini file with unquoted string values.
To avoid this problem, it's vital that if your program stores any sensitive data in constants, that you either pre-scan the ini file for unquoted strings, or that you do not use this function.
A suitable pre-parse parser might be as follows.
This assumes that there are no non-word (a-zA-Z0-9_) characters in your keys, and minimises whitespace.
It tries to convert intelligently, like so:
value1 = value ; this is a comment
value2 = value; with semicolon in
to
value1 = "value" ; this is a comment
value2 = "value; with semicolon in"
<?php
$file = file_get_contents('user_provided.ini');
$file2 = preg_replace('/^
(\s*\w+\s*=\s*) # Part \1 - the key and initial whitespace.
( # Part \2 - the value to be quoted
(?:(?!\s;)[^"\r\n]) # Anything but \r, ", \s;, \n
*? # As little as possible of that, minimise whitespace.
)
( # Part \3 - everything after the value
\s* # Optional whitespace.
(?:\s;.*)? # Optional comment preceded by a space
)
$/mx', '\1"\2"\3', $file);
file_put_contents('user_provided.ini2', $file2);
?>
Julio L
|