Whenever we move the project from localhost to development or production, changing the environment paths, database server name, databaseuser name, database password , is a tedious process and i always got bugged. We are over cautious of over writing the environment variables. Here is a quick fix for that.

Lets write a configuration file( config.ini ) which consists of all the environment settings -

[localhost]
application_path  =  c:/php/htdocs/myproject/
db_host           =  localhost
db_user           =  my_db_user
db_password       =  my_db_password
db_name           =  my_database

[staging]
application_path  = /development/html/myproject/
db_host           = server.com
db_username       = my_devel_db_user
db_password       = my_devel_db_password
db_name           = my_devel_db_name

[production]
application_path  = /production/html/myproject/
db_host           = server.com
db_username       = my_prod_db_user
db_password       = my_prod_db_password
db_name           = my_prod_db_name

Now lets write a Bootstrap file( index.php) which uses the configuration,

<?php

define("OPEARTING_MODE",  "localhost");
// For development,  define("OPEARTING_MODE",  "staging");
// For production, define("OPEARTING_MODE",  "production");

// Parse the ini file by blocks
$configData = parse_ini_file("config.ini",  true);

require_once $configData[OPERATING_MODE]["application_path"] . "file.php";

define(DB_USER,         $configData[OPERATING_MODE]["db_user"])

define(DB_HOST,         $configData[OPERATING_MODE]["db_host"]);

define(DB_PASSWORD,	$configData[OPERATING_MODE]["db_password"]);

define(DB_NAME,         $configData[OPERATING_MODE]["db_name"]);

mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
?>

Just by changing the operating mode, we can easily configure the environment. Tedious process of manually editing each and every environment settings after the file is uploaded/checked out to development/production can be avoided by this approach.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Simpy
  • StumbleUpon
  • Technorati
  • YahooMyWeb