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 *********/
$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.
; 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
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
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
// 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
//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
//assign different values from the file to different variables
//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 ";
// 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!");
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.