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.

Parsing ini configuration files using php

Introduction

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 *********/
$admin_email="[email protected]";
$file="logs/contact.log";
$success_msg="Thanks! your submission was recieved, we will get back to you soon!";
/****** E N D ****************/

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
;The basic element contained in an INI file is the parameter.
;Every parameter has a name and a value, delimited by an equals sign (=).
; The name appears to the left of the equals sign.
name=value
; Parameters can be grouped into arbitrarily-named sections.
; The section name appears on a line by itself, in square brackets ([ and ]).
; All parameters after the section declaration are associated with that section.
;here is an example section
[database-info]
host=localhost
username=joe-dahn
password=123123123

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 .
file_name
The name of the ini file to be parsed, including its path.
$process-sections-boolean
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 :

;Updated by John Doe
[general]
[email protected]
log_file=contact.log
[misc]
admin_url=www.example.com
admin_name=mujtaba

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
$file="configuration.ini" ;
// Lets check if the file exists and is readable by our script
if (file_exists($file) && is_readable($file))
{
//yay... the file is readable and exists
//lets parse it into an array
$settings=parse_ini_file($file);
//At this point, all the data from the ini file is
//stored in the $settings array... just for the sake of
//convenience you can print it out using
//print_r($settings);
//assign different values from the file to different variables
$email=$settings["admin_email"];
$log_file=$settings["log_file"];
$url=$settings["admin_url"];
$name=$settings["admin_name"];
//echo it! or use it in any other way you wish
echo "Yay... Email ID of $name is $email , his url is $url and the log file path is $log_file ";
}
else
{
// If the configuration file does not exist or is not readable, DIE php DIE!
die("Sorry, the $file file doesnt seem to exist or is not readable!");
}

Conclusion

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 :
http://en.wikipedia.org/wiki/INI_file

parse_ini_file() manual on php.net :
http://us.php.net/manual/en/function.parse-ini-file.php


[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.]

4 Comments

balu September 28, 2011

how can i change the values of .ini file in php

balu September 28, 2011

how can i change the values of .ini file in php dynamically

Toufik February 14, 2013

good work, thank you for helping