[en] How To Use Custom ini Files With PHP

quote from: www.dev-tips.com

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"

Herkesle Paylaşın:
  • Print
  • del.icio.us
  • Facebook
  • email
  • FriendFeed
  • Identi.ca
  • LinkedIn
  • MySpace
  • PDF
  • StumbleUpon
  • Twitter
You can leave a response, or trackback from your own site.

Leave a Reply

Powered by WordPress | Designed by: free Drupal themes | Thanks to hostgator coupon and cheap hosting