How To Use Custom ini Files With PHP
Wednesday, June 3rd, 2009
The special 'config' file is a very important file at the heart of almost every web/software application. The config file typically holds information that will be used over and over again, such as database info. There are plenty of ways you can go about creating a config file. Many just create a new file with some defined constants and include the file. Others use a database or XML file for config storage. Today, we will have a look at a simple example of using a custom ini file to set our preferences.
The config.ini file
Setting up a config.ini file is super simple. Create a new file on your server named 'config.ini' and type in something like below:
;This is a basic ini file setup ; ;Note how comments are made with semi colons? [version_info] version_number = 3.0 version_stable = true last_updated = 06/23/08 [db_info] db_name = MyDatabase db_user = root db_pass = root
The PHP file
Now all we need to do is parse the contents of the file, we can do this using the parse_ini_file() function.
<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
//Parse and store the ini file, this will return an associative array
$config_info = parse_ini_file('config.ini', true);
//Debug
print_r($config_info);
//Example usage
echo 'You are currently running version ' . $config_info['version_info']['version_number'];
?>
This would output You are currently running version 3.0
Other options
As I said, this is just one way to store important configuration data. A few other options include, but are not limited to:
- XML File
- PHP File
- Database Storage (obviously where applicable)
Simple right? Obviously, this is a super simplified example, but you can see how easy and helpful a basic .ini file can be! What are some of your methods for storing config preferences?
If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!









Great article! Thanks!
I’ve never used ini files for config values in php. Can you call ini files to your browser and do they appear in plain-text? If so; be careful and don’t put the file anywhere in you web root.
Hey Gaya,
Your absolutely right about being careful with this on a live server, I think the same goes for any configuration style, just make sure the appropriate permissions and precautions are set and you’re good to go. Thanks for your comment buddy
This a great introduction. Do you have any more in depth articles on doing this, such as with a php file, and any other best practices? I just started learning PHP and surprisingly am having trouble finding good help. Something like a config.php file, or in wordpress I think it’s called wp-config.php
Thanks Eric, glad you liked the article.
You’ve given me a great idea for a future article, stay tuned!