Php define configuration file

Php define configuration file

The configuration file ( php.ini ) is read when PHP starts up. For the server module versions of PHP, this happens only once when the web server is started. For the CGI and CLI versions, it happens on every invocation.

  • SAPI module specific location ( PHPIniDir directive in Apache 2, -c command line option in CGI and CLI)
  • The PHPRC environment variable.
  • The location of the php.ini file can be set for different versions of PHP. The root of the registry keys depends on 32- or 64-bitness of the installed OS and PHP. For 32-bit PHP on a 32-bit OS or a 64-bit PHP on a 64-bit OS use [(HKEY_LOCAL_MACHINE\SOFTWARE\PHP] for 32-bit version of PHP on a 64-bit OS use [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\PHP] ] instead. For same bitness installation the following registry keys are examined in order: [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y.z] , [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y] and [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x] , where x, y and z mean the PHP major, minor and release versions. For 32 bit versions of PHP on a 64 bit OS the following registry keys are examined in order: [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x.y.z] , [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x.y] and [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x] , where x, y and z mean the PHP major, minor and release versions. If there is a value for IniFilePath in any of these keys, the first one found will be used as the location of the php.ini (Windows only).
  • [HKEY_LOCAL_MACHINE\SOFTWARE\PHP] or [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\PHP] , value of IniFilePath (Windows only).
  • Current working directory (except CLI).
  • The web server’s directory (for SAPI modules), or directory of PHP (otherwise in Windows).
  • Windows directory ( C:\windows or C:\winnt ) (for Windows), or —with-config-file-path compile time option.

If php-SAPI.ini exists (where SAPI is the SAPI in use, so, for example, php-cli.ini or php-apache.ini ), it is used instead of php.ini . The SAPI name can be determined with php_sapi_name() .

Note:

The Apache web server changes the directory to root at startup, causing PHP to attempt to read php.ini from the root filesystem if it exists.

Using environment variables can be used in php.ini as shown below.

Example #1 php.ini Environment Variables

; PHP_MEMORY_LIMIT is taken from environment memory_limit = $

The php.ini directives handled by extensions are documented on the respective pages of the extensions themselves. A list of the core directives is available in the appendix. Not all PHP directives are necessarily documented in this manual: for a complete list of directives available in your PHP version, please read your well commented php.ini file. Alternatively, you may find » the latest php.ini from Git helpful too.

Example #2 php.ini example

; any text on a line after an unquoted semicolon (;) is ignored [php] ; section markers (text within square brackets) are also ignored ; Boolean values can be set to either: ; true, on, yes ; or false, off, no, none register_globals = off track_errors = yes ; you can enclose strings in double-quotes include_path = ".:/usr/local/lib/php" ; backslashes are treated the same as any other character include_path = ".;c:\php\lib"

It is possible to refer to existing .ini variables from within .ini files. Example: open_basedir = $ «:/new/dir» .

Читайте также:  Метод минимальных поправок python

Scan directories

It is possible to configure PHP to scan for .ini files in a directory after reading php.ini . This can be done at compile time by setting the —with-config-file-scan-dir option. The scan directory can then be overridden at run time by setting the PHP_INI_SCAN_DIR environment variable.

It is possible to scan multiple directories by separating them with the platform-specific path separator ( ; on Windows, NetWare and RISC OS; : on all other platforms; the value PHP is using is available as the PATH_SEPARATOR constant). If a blank directory is given in PHP_INI_SCAN_DIR , PHP will also scan the directory given at compile time via —with-config-file-scan-dir.

Within each directory, PHP will scan all files ending in .ini in alphabetical order. A list of the files that were loaded, and in what order, is available by calling php_ini_scanned_files() , or by running PHP with the —ini option.

Assuming PHP is configured with --with-config-file-scan-dir=/etc/php.d, and that the path separator is . $ php PHP will load all files in /etc/php.d/*.ini as configuration files. $ PHP_INI_SCAN_DIR=/usr/local/etc/php.d php PHP will load all files in /usr/local/etc/php.d/*.ini as configuration files. $ PHP_INI_SCAN_DIR=:/usr/local/etc/php.d php PHP will load all files in /etc/php.d/*.ini, then /usr/local/etc/php.d/*.ini as configuration files. $ PHP_INI_SCAN_DIR=/usr/local/etc/php.d: php PHP will load all files in /usr/local/etc/php.d/*.ini, then /etc/php.d/*.ini as configuration files.

Источник

Create Config Files in PHP

Create Config Files in PHP

  1. Store the Configurations in an Array in a PHP File
  2. Typecast the Array Configuration to an Object in a PHP File
  3. 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.

Читайте также:  Java comparator with double

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.

Источник

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 article we have shown how to read configuration files in PHP with hassankhan/config package.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

Источник

Оцените статью