Php web configuration file

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

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"));

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']);

Источник

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 

Источник

Читайте также:  Php mixed что за тип
Оцените статью