Config files: PHP or INI?
Moderator: General Moderators
Config files: PHP or INI?
Hey all,
I briefly ran across a code example a while ago that used an INI file to store its configuration, which got me thinking...
With the parse_ini_file() function, we can have PHP read through the INI file and store its values in an array, which we can access using offsets. However, using an INI file for a site's configuration strikes me as odd, as you'd have to use PHP to get those values anyway. So, why not just use PHP from the start?
I have two questions regarding this issue.
1.) Is there any benefit to using an INI file for a site's configuration over just a PHP file?
2.) What are some other reasons to use INI files in a website?
I briefly ran across a code example a while ago that used an INI file to store its configuration, which got me thinking...
With the parse_ini_file() function, we can have PHP read through the INI file and store its values in an array, which we can access using offsets. However, using an INI file for a site's configuration strikes me as odd, as you'd have to use PHP to get those values anyway. So, why not just use PHP from the start?
I have two questions regarding this issue.
1.) Is there any benefit to using an INI file for a site's configuration over just a PHP file?
2.) What are some other reasons to use INI files in a website?
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: Config files: PHP or INI?
Two reasons:So, why not just use PHP from the start?
1. INI files are more than just read only data, they are intended to be stored in an array and changed at runtime. While reconstructing a PHP configuration file wouldn't be rocket science, if you later add some code to that file, you'd probalby lose it and break your configuration include.
Writing to PHP files is not a good practice, because it requires write access on the file, allowing potential hackers the ability to execute arbitrary code on your server.
2. Performance. Parsing an INI file is typically faster than a PHP file. In fact I would bet it would win in a straight comparison 100% of the time. The structure of the file is simple compared to PHP.
Lastly although less important, is portability. If you ever change the language from PHP to Python, it's just one less file you have to worry about porting over. Use PHP what it's intended for, a imperative programming language, not a configuration storage medium.
Cheers,
Alex
Re: Config files: PHP or INI?
I tend to favor using a database for most preferences/configurations - however, sometimes a text-based config file is unavoidable. To do this I usually create a PHP file that doubles as an INI - config.ini.php. It looks like this:
This way if someone tries to access your config file through their browser it will be parsed as PHP but the first line automatically kills any output. But, when you try to parse it as an INI file, the parser will see the ; on the first line, assume its a comment, and then move on.
Code: Select all
;<?php die("Don't peek at my INI."); ?>
;Config File
db = mysql
base_dir = "/superdotNET"
;?>Re: Config files: PHP or INI?
If you're looking for performance you'll probably find that caching your configs will improve your overall performance.
I store my configurations in a database and populate APC (http://www.php.net/apc) cache from it. That way I can make numerous changes to my config and then just clear the cache to repopulate my configurations.
APC also handles mass define quite nicely (http://www.php.net/manual/en/function.a ... stants.php).
If you need something that external applications can access, you can try Memcache.
I store my configurations in a database and populate APC (http://www.php.net/apc) cache from it. That way I can make numerous changes to my config and then just clear the cache to repopulate my configurations.
APC also handles mass define quite nicely (http://www.php.net/manual/en/function.a ... stants.php).
If you need something that external applications can access, you can try Memcache.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Config files: PHP or INI?
I think some people just like Domain Specific Languages like INI files, while other just use PHP for everything. If you were going to write a tool to generate them then maybe INI makes more sense, but you could generate PHP just as easily.
As for performance, I doubt that there is much difference. The INI file needs code to parse it, whereas the PHP file is the config array.
As for performance, I doubt that there is much difference. The INI file needs code to parse it, whereas the PHP file is the config array.
(#10850)
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Config files: PHP or INI?
JSON is also handy (json_encode()). Fast to parse, allows run-time modifications and "PHP data" through serialization.
- PHPHorizons
- Forum Contributor
- Posts: 175
- Joined: Mon Sep 14, 2009 11:38 pm
Re: Config files: PHP or INI?
In my latest site, I started out going the json route and dynamically creating constants. There's one big problem that I ran into with this: no code assist!
So I have switched over to using a Name\Space\Config class that contains a bunch of class constants. Now I get gobs of code assist
and I don't dirty up the global namespace.
As far as I know, class constants can't be declared at run time, so declaring constants would have be constants of the global variety.
On any page that needs a Config value, I do have to use a use statement use Name\Space; and then I refer to them this way: Space\Config::Constant
(naturally Name\Space should be Company\Product or something more meaningful)
If using php < 5.3, I would suggest using a class name of something like MyConfig.
So I have switched over to using a Name\Space\Config class that contains a bunch of class constants. Now I get gobs of code assist
As far as I know, class constants can't be declared at run time, so declaring constants would have be constants of the global variety.
On any page that needs a Config value, I do have to use a use statement use Name\Space; and then I refer to them this way: Space\Config::Constant
(naturally Name\Space should be Company\Product or something more meaningful)
If using php < 5.3, I would suggest using a class name of something like MyConfig.
Re: Config files: PHP or INI?
Your script isn't the only one writing to that file. Why would you force your users to learn PHP or JSON?
- PHPHorizons
- Forum Contributor
- Posts: 175
- Joined: Mon Sep 14, 2009 11:38 pm
Re: Config files: PHP or INI?
By that reasoning, why force your users to learn the configuration settings!josh wrote:Your script isn't the only one writing to that file. Why would you force your users to learn PHP or JSON?
That's basically what is being said here.
Code: Select all
{
"foo": 1,
"bar": 2
}
Code: Select all
foo = 1
bar = 1
Code: Select all
<?php
class Config {
const foo = 1;
const bar = 1;
}
I think that given any of those three options, any competent person would be able to edit the settings. PHP would be the most interesting looking, but any competent person could handle it.
Obviously storing settings in a db is great (hm, the database configuration settings...) but doesn't always work.
The moral of the story, as is the case many times, is use the right tool for the job. text, php, json, xml all have proper uses for ini settings. There are advantages and disadvantages to both.
Cheers
Re: Config files: PHP or INI?
Dude, some of my clients type website addresses into google instead of their address bar. By the same logic I could say any competent person could handle writing their own app instead of buying yours.PHPHorizons wrote:but any competent person could handle it.
.ini is a lot more common then .php or json, even windows uses it
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Config files: PHP or INI?
I can't imagine letting non-technical users anywhere near any configuration file. If they have some technical skill, I don't think that any of the formats above are less brittle than one of the others.
(#10850)
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Config files: PHP or INI?
I rather provide an interface that lets them modify the configuration file. That way it would be easier plus I could validate their inputs and disallow modification of certain configuration values.
Re: Config files: PHP or INI?
That is my "preference" too, but when your $$$ comes in based on front-end features, you gotta manage how much time you spend on 'screens' that users will hardly ever use. Plus having it in the control panel can pose security issues, not only do you have to build those screens you have to figure out how to control access to them now, it can open up a whole can of worms.kaisellgren wrote:I rather provide an interface that lets them modify the configuration file. That way it would be easier plus I could validate their inputs and disallow modification of certain configuration values.
I should also note, anyone can grab the Zend_Config component, and instantiate it with just about any format (although I can't say I've heard of JSON being used for configurations before, maybe caching them.) After that you can use the Zend_Config API to get at the configuration.
The reason I think JSON is a bad choice is not a lot of developers even know about it. Someone could go in and copy a line of the configuration file and leave out a ",". With ini the lines are more or less reversible. So someone could copy something out of your documentation that had a comma at the end, and put it last, for example. With ini it doesn't matter if you have a ; , or } at the end of not. There's also less overall distracting syntactical symbols (whitespace is used instead).
We all take things like escaping values going into SQL and HTML code for granted, I think this is one of those situations where we're taking for granted a basic understanding of formal languages, that some users simply just do not have
For instance I challenge anyone to answer every question someone could have about the following format:
There's so many tech support requests that could come from having this in a config file.# <?php
# class Config {
User: "I put >?php" and it doesnt work
User: What is a class?
User: If I don't put the class { part does it still work?
...
Users will break their stuff in stupid ways
If you need more convincing I suggest lurking on a sendmail mailing list for a few weeks
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Config files: PHP or INI?
PHP provides a simple solution:josh wrote:Also the JSON spec does not specify any formatting, what happens when your program modifies a setting and it kills all the spacing?
Code: Select all
// Updating the config mysql hostname through an UI
$config = json_decode(file_get_contents('config'));
$config['mysql']['hostname'] = $_GET['hostname']; // apply any logic here
file_put_contents(json_encode($config),'config');
I guess it comes down to the purpose of the software. For sure INI files are easier to modify by hand then some JSON.
Re: Config files: PHP or INI?
Yeah totally, its not always going to be the solution but its by far easier to edit. For Nth level arrays I would probably either build it into the GUI, or they would create settings named according to a pattern, likekaisellgren wrote: The question is, how would you achieve n-level associative arrays with INI files?
setting-1
setting-2
setting-3
(then in PHP you would turn it into an array just called "setting"). I like apache's hybrid, you can just list settings one per line, but you can also have sections within "directives" that look like XML tags, its kinda neat. definitely use friendly