Parsing INI Files Using PHP

Parsing ini configuration 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.
<br /> /******* S E T T I N G S *********/<br /> $admin_email="[email protected]";<br /> $file="logs/contact.log";<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 :
<br /> ; 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 /> name=value<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 /> [database-info]<br /> host=localhost<br /> username=joe-dahn<br /> password=123123123<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

Example Code

Consider an ini file called “configurations.ini” with the following content :
<br /> ;Updated by John Doe<br /> [general]<br /> admin_email=[email protected]<br /> log_file=contact.log<br /> [misc]<br /><br /> admin_name=mujtaba<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.

<br /> // 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 /> {<br /> //yay... the file is readable and exists<br /> //lets parse it into an array<br /> $settings=parse_ini_file($file);<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 /> //print_r($settings);<br /> //assign different values from the file to different variables<br /> $email=$settings["admin_email"];<br /> $log_file=$settings["log_file"];<br /> $url=$settings["admin_url"];<br /> $name=$settings["admin_name"];<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 /> }<br /> else<br /> {<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 /> }<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.

Download source code files

Further reading :

Wikipedia article on INI files :

parse_ini_file() manual on :

[Editor’s Note: This post is submitted by our guest blogger Mujtaba Ahmed, an avid web designer, developer and a WordPress and jquery enthusiast.

[Editor’s Note: This post is submitted by our guest blogger Mujtaba Ahmed, an avid web designer, developer and a wordpress and jquery enthusiast.

If you, too would like to write for Devils Workshop, please check this. Details about our revenue sharing programs are here.]

About Author

Mujtaba Ahmed

Mujtaba Ahmed

A tenn harstruck with technology, an avid web designer and developer and a wordpress and jquery enthusiast