Parsing INI Files Using PHP
Many times during development involving php, one would want to store some variables as settings. The most common way developers do this is by defining those variables at the beginning of the script. For example, while writing a contact form script, I would like to define a few handy variables first, including admin email, contact log file name/path and etc… The most common way to do this would be to write something similar in your php script.
/******* S E T T I N G S *********/<br />
$admin_email="[email protected]";<br />
$success_msg="Thanks! your submission was recieved, we will get back to you soon!";<br />
/****** E N D ****************/<br />
While this method is pretty clean and simple, it can lead to a mess in situations such as when the script is to be configured by un-experienced or non-techy users. Consider the situation when non-techy “Joe” updates this script. He changes the $success_msg variable value to something like : *Whoa! i recieved your message ! Thanks for contactin me.. –“Joe” * . Notice the un-escaped “” ? That would lead to a parse error and the script will cease to work. (and poor Joe will be left scratching his head) .
This is where ini files can come in real handy . Lets first see what are ini files . Wikipedia defines ini files as :
The INI file format is a de facto standard for configuration files. INI files are simple text files with a basic structure. … The name “INI file” comes from the filename extension usually used, “.INI”, that stands for “initialization”. Sometimes, files using the INI file format will use a different extension, such as “.CFG”, “.conf”, or “.TXT”
Basic structure of an ini file
The basic format of an ini file is :
; Some comment, notice that semi-colons are used for comments<br />
;The basic element contained in an INI file is the parameter.<br />
;Every parameter has a name and a value, delimited by an equals sign (=).<br />
; The name appears to the left of the equals sign.<br />
; Parameters can be grouped into arbitrarily-named sections.<br />
; The section name appears on a line by itself, in square brackets ([ and ]).<br />
; All parameters after the section declaration are associated with that section.<br />
;here is an example section<br />
Parsing ini file using php
php has a very nice function named parse_ini_file($file_name,$process-sections-boolean) to parse ini files.
The function accepts two arguments .
The name of the ini file to be parsed, including its path.
Boolean (true/false) for parsing different sections of ini file into a multi-dimensional array. Default is false
Consider an ini file called “configurations.ini” with the following content :
;Updated by John Doe<br />
[email protected]<br />
Now we will write a php script to read and parse the contents of this file using the “parse_ini_file()” function.
Most of the lines are commented so you can know what is happening.
// The name and path of our configuration file<br />
$file="configuration.ini" ;<br />
// Lets check if the file exists and is readable by our script<br />
if (file_exists($file) && is_readable($file))<br />
//yay... the file is readable and exists<br />
//lets parse it into an array<br />
//At this point, all the data from the ini file is<br />
//stored in the $settings array... just for the sake of<br />
//convenience you can print it out using<br />
//assign different values from the file to different variables<br />
//echo it! or use it in any other way you wish<br />
echo "Yay... Email ID of $name is $email , his url is $url and the log file path is $log_file ";<br />
// If the configuration file does not exist or is not readable, DIE php DIE!<br />
die("Sorry, the $file file doesnt seem to exist or is not readable!");<br />
As you see, this method is lot more convenient than the common way of defining variables in the top in your php script. While this can be used in real life situations, it will always be best to chose a complex name for your configuration file that cannot be guessed.. or better still consider setting up .htaccess rules to prevent the ini file from direct viewing by visitors, but that is out of the scope of this tutorial :).
Hope you have enjoyed reading this as much as i did writing it.
Further reading :
Wikipedia article on INI files :
parse_ini_file() manual on php.net :
[Editor’s Note: This post is submitted by our guest blogger Mujtaba Ahmed, an avid web designer, developer and a WordPress and jquery enthusiast.