Read Configuration Setting from php.ini

In any application development, design of configuration files are very important, because there are different server connection reference are used in several times in application coding, so when we deploy application from one environment to another environment (like dev to staging server, then production server), all the configuration values will change.

Therefore, to make the change process easy, we keep all type of configuration values in one place. Here you learn how to read configuration setting in php application.

Php configuration file settings

Php.ini file contain system configuration related information in plain text format, the file reside in root of php folder.

Here are some system configuration information written in php.ini file

[Syslog]
define_syslog_variables=Off
[Session]
define_syslog_variables=Off
[Date]
date.timezone=Europe/Berlin
[MySQL]
mysql.allow_local_infile=On
mysql.allow_persistent=On
mysql.cache_size=2000
mysql.max_persistent=-1
mysql.max_link=-1
mysql.default_port=3306
mysql.default_socket="MySQL"
mysql.connect_timeout=3
mysql.trace_mode=Off
[Sybase-CT]
sybct.allow_persistent=On
sybct.max_persistent=-1
sybct.max_links=-1
sybct.min_server_severity=10
sybct.min_client_severity=10
[MSSQL]
mssql.allow_persistent=On
mssql.max_persistent=-1
mssql.max_links=-1
mssql.min_error_severity=10
mssql.min_message_severity=10
mssql.compatability_mode=Off
mssql.secure_connection=Off
httpd.conf file in Apache

We can store any application configuration related information like database details, SMTP details, or any other template related information.

This is how you can store application related information in httpd.conf file

php_value mysql.default.user      myusername
php_value mysql.default.password  mypassword
php_value mysql.default.host      myserver

Now to retrieve information in php code we can use the same keys

 
$db = mysqli_connect(ini_get("mysql.default.user"),
                     ini_get("mysql.default.password"),
                     ini_get("mysql.default.host"));
 
Using config.ini in php

We can also store all configuration related information in config.ini file.

[database]
servername = myservername
username = mydbuser
password = mypassword
dbname = myDBName

Now in php code we can add the file reference, then retrieve the configuration related information using right keys.

Here is how you can read configuration information in php from config.ini file.

$config = parse_ini_file('../fodlername/config.ini'); 
$connection = mysqli_connect($config['servername'],$config['username'],$config['password'],$config['dbname']);
 
Php Configuration Setting
Learn php programming, php web development with free tutorials
Other Popular Tutorials
PHP Examples | Join PHP Online Course