- Controlling PHP settings with a custom php.ini file
- Setting up a custom php.ini file
- PHP config
- Setting up PHP config
- PHP config JSON example
- PHP YAML example
- Merging configuration files
- Code configuration with AbstractConfig
- Create Config Files in PHP
- Store the Configurations in an Array in a PHP File
- Typecast the Array Configuration to an Object in a PHP File
- Use an INI File to Create a Config File in PHP
- How to define your own PHP configuration
- You may also be interested in
- Related Posts
- Scan Your Docker Images and Containers with VirusTotal: A Step-by-Step Guide
- Accelerate Your Performance Testing on Ubuntu with k6 and Postman-to-k6
- Solve the “Cannot read properties of undefined (reading ‘type’)” error with these simple fixes
- About Anto Online
Controlling PHP settings with a custom php.ini file
This article describes how to use custom php.ini files to control PHP settings for your website.
Setting up a custom php.ini file
The php.ini file contains directives to control many PHP settings. You can set up one or more custom php.ini files to define how your website works. For example, you might have a custom file in the /home/username/public_html directory for your website’s main pages, and a separate custom file in the /home/username/public_html/images directory for your website’s image files.
Please follow these steps to set up a custom php.ini file:
- Log in to cPanel.
- Using the File Manager, navigate to the directory where you want to change the behavior of php. By default, a user’s php.ini file only affects the directory it is in.
- To create a new file сlick the File button in the File Manager toolbar, and name this file it php.ini.
- To edit the new php.ini click on the new file and then in the File Manager toolbar click the Edit button.
- Add php directives to the new php.ini, after then click the Save Changes button.
- After creating the php.ini file and setting its permissions, you can verify that the configuration is active. You can do this by creating a phpinfo file in the directory where you created the php.ini
- Load the phpinfo file into your web browser and look for one of the directives you installed. Make sure the changes are displayed.
- You can create custom php.ini files for each directory in your website, but also you can configure settings in one php.ini file and then apply it to the entire web site. To do this, add the following line to the .htaccess file in your website’s document root directory. Replace username with your Beehosting username:
lsapi_phpini /home/username/public_html/php.ini
PHP config
PHP config tutorial shows how to create configuration files in PHP. It uses the hassankhan/config package.
$ php -v php -v PHP 8.1.2 (cli) (built: Aug 8 2022 07:28:23) (NTS) .
The hassankhan/config is a lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files. If we work with YAML files, we need to install symfony/yaml package.
Setting up PHP config
First, we install the necessary packages.
$ composer req hassankhan/config symfony/yaml
We install two packages with composer.
This is composer.json file. We also enable autoloading.
PHP config JSON example
In the first example, we read the configuration data from a JSON file.
We have db.json in the config directory.
get('app.port') . "\n"; echo $conf->get('db.host') . "\n"; echo $conf->get('db.port') . "\n"; echo $conf->get('db.name') . "\n";
We load the configuration file either with Config::load or Config . The values are retrieved with the get method. The dot character is used to go through the hierarchy of attributes.
$ php read_json_cfg.php 3000 localhost 27017 ydb
PHP YAML example
In the second example, we read configuration data from a YAML file.
app: port: 3000 db: host: localhost port: 27017 name: ydb
get('app.port') . "\n"; echo $conf->get('db.host') . "\n"; echo $conf->get('db.port') . "\n"; echo $conf->get('db.name') . "\n";
The example reads configuration file from the db.yaml file.
$conf = new Config('config/db.yaml', new Yaml);
In the second parameter, we provide the configuration parser.
$ php read_yaml_cfg.php 3000 localhost 27017 ydb
Merging configuration files
The merge method groups configuration files.
app: port: 3000 db: host: localhost port: 27017 name: ydb
This is the first configuration file.
This is the second configuration file.
merge($conf2); echo $conf->get('db.port') . "\n"; echo $conf->get('db.name') . "\n"; echo $conf->get('version') . "\n";
In the example we merge the two configuration files. We can access attributes from both files with one object.
Code configuration with AbstractConfig
We can specify the configuration details in code by using AbstractConfig .
'localhost', 'port' => 80, 'servers' => [ 'host1', 'host2', 'host3' ] ]; > >
The configuration is specified in the AbstractConfig’s getDefaults file.
get('host') . "\n"; echo $conf->get('port') . "\n"; echo $conf->get('servers')[0] . "\n";
The example reads the configuration from the code.
In this tutorial, we have shown how to read configuration files in PHP with hassankhan/config package.
Create Config Files in PHP
- Store the Configurations in an Array in a PHP File
- Typecast the Array Configuration to an Object in a PHP File
- Use an INI File to Create a Config File in PHP
This article will introduce some methods to create config files in PHP.
Store the Configurations in an Array in a PHP File
We can create an array of the configuration items to form a config file in PHP.
An associative array will be best for storing the configuration data. The file can be included in another PHP file, and the configuration can be used.
We can use the include() function to have the configuration file. We will demonstrate the creation of config files for the database connection in PHP.
For example, create a PHP file config.php and create an array. Write the keys hostname , username , and password in the array.
Set the hostname to localhost , username , and password to your database username and password. Then return the array.
Next, create another file, DB.php , and use the include() function to include the config.php file and store it in the $conf variable. Create the variables $hostname , $username , $password , and $db to assign the configurations.
Store the configurations in the variables accessing each array items of the $conf variable. Set your database name in the $db variable. Use the mysqli_connect() function to connect the server and the database.
Provide the variables we created above as the parameters to the mysqli_connect() function. If the correct configuration is provided in the config.php file, the example below will connect the server and the database.
In this way, we can use an array to create a configuration file in PHP.
return array( 'hostname' => 'localhost', 'username' => 'root', 'password' => 'pass123' );
$conf = include('config.php'); $hostname = $conf['hostname']; $username = $conf['username']; $password = $conf['password']; $db = "my_db"; $con = mysqli_connect($hostname, $username, $password,$db); if (!$con) die("Failed to establish connection"); > echo "Connection established successfully";
Connection established successfully
Typecast the Array Configuration to an Object in a PHP File
This method will typecast the array in the config.php file into an object. In this way, the configurations can be accessed as an object in the PHP file.
Furthermore, we can take the benefits from objects for data handling purposes. For example, the objects could easily be passed JSON to the client-side if we use JavaScript.
For example, typecast the array placing (object) before the array function after the return keyword. Then, in index.php , access the configurations as $conf->hostname as shown in the example below.
return (object) array ( 'hostname' => 'localhost', 'username' => 'root', 'password' => 'subodh', 'db' => 'oop' );
$conf = include('config.php'); $hostname = $conf->hostname; $username = $conf->username; $password = $conf->password; $db = $conf->db; $con = mysqli_connect($hostname, $username, $password,$db); if (!$con) die("Failed to establish connection"); > echo "Connection established successfully";
Use an INI File to Create a Config File in PHP
We can also create a config file using the INI file in PHP. We can store all the configurations in the INI file and use the parse_ini_file() function to access those configurations in the PHP file.
The INI file is broadly used as a configuration file. The structure of the file contains key-value pairs.
The parse_ini_file loads the content of the INI file as an associative array. We will demonstrate the creation of the INI config file to establish a connection to the server and the database.
For example, create a file config.ini and write the keys hostname , username , password , and db . Then, write the correct configurations. An example is shown below.
hostname = 'localhost' username = 'root' password = 'pass1234' db = 'my_db'
Next, create an index.php file and create an $ini variable in it.
Use the parse_ini_file() function to parse the config.ini file. The $ini variable contains an associative array of the configurations.
Now use the mysqli_connect() function with the correct parameters to establish the connection as we did above.
$ini = parse_ini_file('config.ini'); $hostname = $ini['hostname']; $username = $ini['username']; $password = $ini['password']; $db = $ini['db']; $con = mysqli_connect($hostname, $username, $password,$db); if (!$con) die("Failed to establish connection"); > echo "Connection established successfully";
Connection established successfully
It should be noted that the INI file should be kept in a non-public folder so that we do not encounter any security problems. In this way, we can create a configuration file with an INI file.
Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.
How to define your own PHP configuration
This post will show how you can define a custom PHP configuration for your website. A Custom PHP configuration will allow you to change server default configurations. This post mentions two options for adjusting configuration: user.ini and .htaccess.
The default server configuration (set using the php.ini file) will prevent your website from displaying errors. This setting in the php.ini is excellent in production environments but is cumbersome in a development environment.
Before uploading the .user.ini file:
You may also be interested in
Anto’s editorial team loves the cloud as much as you! Each member of Anto’s editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.
Support Anto Online and buy us a coffee. Anything is possible with coffee and code.
Related Posts
Scan Your Docker Images and Containers with VirusTotal: A Step-by-Step Guide
Accelerate Your Performance Testing on Ubuntu with k6 and Postman-to-k6
Solve the “Cannot read properties of undefined (reading ‘type’)” error with these simple fixes
About Anto Online
Having started his career in 1999 as a Desktop Support Engineer, Anto soon changed paths and became a developer. After several years of development experience, he transitioned into a consultant. As an enterprise application consultant for a leading SaaS software provider, Anto specializes in AWS’s serverless technologies. By day, Anto focuses on helping customers leverage the power of serverless technologies. By night, he indulges his passion for cloud computing by playing with Python and trying out things that are currently beyond the scope of his work. Sometimes Anto needs help as there are not enough hours at night. So Anto relies on a team of fellow Cloud enthusiasts to help him out. Each one is a Cloud expert in their own right, and Anto takes great pride in helping them learn and grow. View all posts by Anto Online →