Viewed   88 times

I want to create a config file for my PHP project, but I'm not sure what the best way to do this is.

I have 3 ideas so far.

1-Use Variable

$config['hostname'] = "localhost";
$config['dbuser'] = "dbuser";
$config['dbpassword'] = "dbpassword";
$config['dbname'] = "dbname";
$config['sitetitle'] = "sitetitle";

2-Use Const

define('DB_NAME', 'test');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
define('TITLE', 'sitetitle');

3-Use Database

I will be using the config in classes so I'm not sure which way would be the best or if there is a better way.

 Answers

5

One simple but elegant way is to create a config.php file (or whatever you call it) that just returns an array:

<?php

return array(
    'host' => 'localhost',
    'username' => 'root',
);

And then:

$configs = include('config.php');
Monday, September 26, 2022
1

If your PHP installation is setup to scan for .ini files you can drop several of them in a folder. If you installed PHP through the Ubuntu repos, it should already be configured this way.

My phpinfo() (note: The additional ini files are the result of installing php extensions from the repos and won't be included with PHP):

The setting for this directory is a compile-time option:

--with-config-file-scan-dir=/etc/php5/apache2/conf.d

You can also set an environment variable through Apache:

SetEnv PHP_INI_SCAN_DIR /php/custom/scan/directory

More info @ ServerFault

Friday, August 5, 2022
 
4

There is also PCLZip, a pure PHP alternative to ZipArchive that can be found at http://www.phpconcept.net/pclzip/

Thursday, August 25, 2022
2

Couple of ways, depending on how you wish to store the config data. The fastest way is to store the data as an .ini file and parse it using PHP's built in parse_ini_file. You could also store the data in other formats such as XML and YAML, but I wouldn't recommend XML as you probably won't be transporting the data betweeen disparate systems, also XML tends to be harder to read with all the extraneous tags (vs yaml or ini).

I personally prefer yaml and use Symfony's Yaml Component parser which will parse the yaml file into an array. Symfony's Yaml Component can be used outside of the Symfony framework. You could also look into Zend's Yaml parser as well.

After you pick the format and parser you wish to use, it's as easy as storing the config file somewhere that is accessible by your webserver, requiring it, and passing it through the parser API. After it is parsed you should have access to the values via an array.

-- Update --

<?php

$settings = parse_ini_file('test.ini');

var_dump($settings);

Results:

array(41) {
["plugins"]=>
string(0) ""
["breadcrumb-navxt"]=>
string(5) "4.0.1"
["bp-moderation"]=>
string(5) "0.1.4"
["buddypress-activity-stream-hashtags"]=>
string(5) "0.4.0"
["buddypress-group-documents"]=>
string(5) "0.3.5"
["buddypress-group-email-subscription"]=>
string(5) "2.9.1"
["buddypress-links"]=>
string(3) "0.5"
["buddypress"]=>
string(6) "1.2.10"
["calendar"]=>
string(5) "1.3.1"
["collapsing-pages"]=>
string(5) "0.6.1"

This appears to work as expected, so if I want the version number of the calendar plug-in I would just do:

var_dump($settings['calendar']);

To store in dynamic variables:

$settings = parse_ini_file('test.ini');

foreach ($settings as $key => $setting) {
    // Notice the double $$, this tells php to create a variable with the same name as key
    $$key = $setting; 
}

var_dump($calendar);
Friday, August 5, 2022
 
3

… PHP scans for INI files in each directory …

This whole manual section is only about .user.ini files.

  • The format is equivalent to the main php.ini

  • This only applies to CGI/FCGI/FPM setups.

… in my case as I'm using Apache web server …

  • No relevancy to Apache/mod_php configurations.

  • The .htaccess config file and php_flag/php_value directives are not INI-style.

  • As you already know, PHP itself doesn't read them. (Well, indirectly. Apache parses them.)

Tell me whether the '.htaccess file'(in case of running PHP as Apache module) is a '.user.ini-style INI file' or not?

  • It's not.

--with-config-file-scan-dirPHP_INI_SCAN_DIR

  • Does also have no relevancy to .user.ini nor .htaccess files.

  • This is used for auxilliary php.ini includes (e.g. module loading/settings, such as /etc/php/7.3/fpm/conf.d/20-gettext.ini / even XAMPP should have something like this)

Saturday, December 10, 2022
 
qortex
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :