Go configure????? Pros/Cons of a config file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mhulse
Forum Commoner
Posts: 41
Joined: Fri Jun 11, 2004 12:50 am

Go configure????? Pros/Cons of a config file?

Post by mhulse »

Hello,

Is this a proper way to write/handle a config file:

Code: Select all

<?php

/********************\
|*  config.inc.php  *|
\********************/

// Location and name of folder to create quotes/bids (no ending /, example: "./some_dir/some_dir":
$config['q_fldr_loc'] = './clients'; 
// Extension of file to create:
$config['doc_type'] = '.php';
// Location to file managing script:
$config['file_mng'] = './mng_files.php';

?>
I would then call it like this:

Code: Select all

<?php include_once($_SERVER['DOCUMENT_ROOT'].'/quotes/inc/config.inc.php'); ?>
And from there, I use the config vars like so:

Code: Select all

...
...
...
$fp = fopen($config['q_fldr_loc'].'/'.$file_name.'/'.$qfname.$config['doc_type'],'wb'); // Open for writing only; If the file does not exist, attempt to create it.
	fwrite($fp,$contents);
	fclose($fp);
	
	echo "<p>Your $".$en_form_num." quote/bid for ".$company." has been generated, <a href='".$config['q_fldr_loc']."/".$file_name."/".$qfname.$config['doc_type']."'>click here</a> to scope it!</p>"."\n";
	echo "Or, <a href='".$config['file_mng']."'>click here</a> to manage/remove directories.";
...
...
...
If my syntax is correct, what are the benifits of using a config file in this way? It seems like a lot of extra code for just declaring global variables? Does anyone have examples of config file coding-formats that they use?

I am not using a mySql DB, does that matter?

Thanks!
Cheers
Micky! ;D
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

it depends how big the whole code base is

if its small, having a config file might be a waste of time.

regardless though, ive always found it usefull to use a config file to make the scripts work right whether on my local box, or the production server. that way i dont need to modify the code before i upload.
you can set things like email address, error reporting, include dir, database info etc...

if ($_SERVER['SERVER_NAME'] === 'localhost') {
// local config
} else {
// prod server config
}


a lot of the benefits come from the ability to make small changes and have it take effect throghout the entire script
and it looks like you see that, after looking at your
" quote/bid for:'.$company.' snippet"
:)
User avatar
Crom
Forum Newbie
Posts: 8
Joined: Mon Nov 08, 2004 2:50 am
Location: Ukraine
Contact:

Post by Crom »

IMHO upon my experience using a separate config is a VERY, VERY good idea. It wil save you a lot of time and head scretching. Why? A lot of times I faced with fact that a small project with couple of files was growing more and more and became a huge project with lots of variables. That's why I loved osCommerce because of its separating of code. My advise - look at their configs and you'll see how strait it is and how it save time when you are changing any value. But of course if you have 3-4 files it will be easier for you to make changes right in them :wink: . As for me I ALWAYS make configs. Even if there are 2 files. Who knows what will happen tomorrow. :D
Inclusion doesn't taking a lot of server time but saves coder's time. I must repeat: IMHO :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Go configure????? Pros/Cons of a config file?

Post by Weirdan »

mhulse wrote:what are the benifits of using a config file in this way? It seems like a lot of extra code for just declaring global variables?
Not a benefit, but a common pitfall: if you have register_globals on one will be able to extend your $config array like this:

Code: Select all

page.php?config&#1111;something]=asd&config&#1111;doc_types]&#1111;]=.exe
Post Reply