- Understanding the Purpose of the web.config File in PHP
- In what directory do i put my mysql config.php files when setting up a website/app?
- PHP Forms Tutorial: Create the Config File
- Configuring phpList #3 — Editing the config.php file
- Use web.config with php
- Writing and editing a PHP config file from a HTML form?
- 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
Understanding the Purpose of the web.config File in PHP
According to them, for website configuration in Windows hosting, one must use the web.config file instead of .htaccess file. This is just a sample, and you can modify it as per your requirements. To store the data of a submitted form in a PHP file, one must use file action functions and open the config.php file. Then, write the necessary PHP code into it.
In what directory do i put my mysql config.php files when setting up a website/app?
As a novice in website creation, I have a designated directory called document root: home/user/public_html/www.example.com/public to store all my static files for public access. However, some of these files contain configurations labeled as config.php , which comprise usernames and passwords. It seems inappropriate to place such sensitive data in the public folder.
Where should these files go?
To change the location of config.php in relation to the static files, you must modify the file path. By default, both files are located in the same directory, so it’s likely you encountered include ‘config.php’; . If you wish to relocate them to a new folder, let’s say it’s called hidden, then you’ll need to update the file path to include ‘hidden/config.php’; .
In case you choose to navigate to the parent directory, it is necessary to utilize the following code: include ‘../config.php’; .
Further information on relative paths can be found in Absolute vs Relative Links .
The wp-config.php file is placed in the document root by WordPress, which is a widely used and large platform. Therefore, it can be inferred that it is suitable for your use as well.
It’s possible to keep the config.php file above the current document root. PHP level and still allow access to it by providing the correct path.
For example, one can refer to the webpage titled «Hardening WordPress» on the «codex.wordpress.org» site for instructions on securing the «wp-config.php» file.
The correct path should be referenced in every file that includes config.php. Storing config.php in the root allows for changing all the includes to use it.
include_once($_SERVER['DOCUMENT_ROOT'] . '/config.php');
Regardless of the file’s location, this will function properly.
Creating a config file in PHP, php file contain all your configs (centralized in one file instead of one for JavaScript and one for PHP). The trick would then be to have
PHP Forms Tutorial: Create the Config File
In this tutorial, you’re going to learn how to create and HTML form that submits data to a MySQL Duration: 2:23
Configuring phpList #3 — Editing the config.php file
Some settings for phpList are available only by logging into the web server and editing the
Duration: 31:08
Use web.config with php
My website, built with PHP, is hosted on a Windows server. When I inquired with the hosting company about the location of the htaccess file file, they informed me that there is no .htaccess file available on Windows hosting. Instead, I was advised to use the web.config file to configure my website.
Upon testing the uncomplicated code in my web.config, I encountered an internal server error with a status code of 500.
The solution was found and it was successful.
Protecting config file of web hosting, Determine what your case. Many way to protect the configurations files, depending of your requirements. When you have custom code (eg: not to
Writing and editing a PHP config file from a HTML form?
I am seeking guidance on saving and editing data entered in an HTML form with multiple fields. Specifically, I want to store the data in PHP config file and allow access for editing by resubmitting the form. Ideally, I would like to accomplish this without utilizing a database.
After the user inputs ‘computer’ as the keyword and picks the color ‘blue’, my intention is to store this information as variables in my config.php file. This way, other pages of my website can retrieve this data. Is it necessary to use an array for this purpose?
Is it possible to prefill the form fields with data from the config.php file upon accessing it again?
Your assistance would be highly valued. Thank you.
To store this type of data in a file, one option is to save all the information in an array with the format of $keyword => $value . Then, you can utilize the serialize() and unserialize() functions to convert the data into a more manageable format that can be effortlessly saved and read from a file.
If you have a single file, any modifications made by a user will impact all users. Therefore, you must devise a method for identifying the user and the appropriate file if this is not acceptable.
A superior approach would be to store the values in a database. You can create a table named options with two fields — option and value — that will hold the configuration options. To assign specific options to particular users, you may include an additional field — userid (which will act as a foreign key to a users table) — to identify the user associated with an option pair.
One way to handle a predefined set of user options is to create a table with fields for each option and default values. Then, you could create a single record for each user and set their specific configuration options within that record.
Your primary PHP script file can contain your configuration file.
Construct the form using a similar approach to this.
Upon submitting the form, you have the option to utilize the fopen() and fwrite() functions to save the entered data into your designated config.php file.
$key = $_POST["key"]; $color = $_POST["color"]; if ($key != '' && $color != '') < $f = fopen('config.php', 'w') or die("can't open file"); fwrite($f, ''); fclose($f); >else < // write default values or show an error message >
There are various options available for achieving this. One of the most efficient methods is to utilize a database like MYSQL as it provides persistence, which is precisely what you require. Give it a try.
$key = $_POST["key"]; $color = $_POST["color"]; mysql_query("INSERT INTO smeTbl VALUES ('1',$key,$color)");
In the configuration file or any other relevant file, you can access and obtain these values.
$query = mysql_query("SELECT * FROM smeTbl WHERE $fetch = mysql_fetch_array($query); $keyword = $fetch["key"]; $color = $fetch["color"];
You have the freedom to tailor this as per your requirements, as this is only a sample.
Upon submitting the form, the form’s data can be stored in a PHP file using the «file action» functions fopen in the config.php file. To display the form, open the config.php file using fopen and utilize the «eval» function to retrieve the data. Please excuse any errors in my English.
//when submit form $string = ''; $fp = fopen('config.php', 'w'); fwrite($fp, $string); fclose($fp); //when display form include("config.php"); //so you can use $keyword and $color
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.