Define config file php

Define config file php

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» .

Читайте также:  Java console in eclipse

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.

Источник

How to define a php config file that define constant and use it any where in project

If you really NEED to have it in PHP format, use var_export() and include, which is on the other hand totally insecure. or use INI files, i.e. create ini file, write values in PHP Solution 3: Don’t use Constants for user settings. Solution 1: There are two ways, define() : The constants you define using define method is available to the whole project.

How to define a php config file that define constant and use it any where in project

how to define a config file that i can use it any where whole project without include every where?

Читайте также:  Размерность массива си шарп

    define() : The constants you define using define method is available to the whole project.

define('SITE_URL' , '/path/to/docroot/'); echo SITE_URL; 
class MyConfig < const SITE_URL = '/path/to/docroot/'; >echo MyConfig::SITE_URL; 

you can define constants in a single config file f.e in config.php and then include it into your project where you need

For using localhost such as XAMPP or WAMP:

  1. Go to php.ini
  2. Find «auto_prepend_file» and change to auto_prepend_file=»xampp/htdocs/dir.php»
  3. Restart your apache service to make it work.
    • Refer to this site for more information

If that didn’t work or you don’t have access to php.ini, try modifying an .htaccess file (I haven’t tried this method yet though)

php_value auto_prepend_file "/real/path/to/file/file_name.php" 

Configuration files — my_config.ini vs my_config.php, So, why use my_config.ini rather than my_config.php? It is not like anyone should be touching it after it is set up and it seems more convenient

How To Work With PHPs Configuration File

In this PHP tutorial, you will learn what the PHP configuration file is, what are some of the Duration: 7:26

PHP configuration settings: where to find them and how to change

▻ Access the full course: https://davehollingworth.net/phpmvcy Learn how to view current PHP Duration: 2:03

How to set up custom php.ini files

This video will show you how to use custom php.ini files to control PHP settings for your web Duration: 9:08

Dynamically write defines in PHP settings file

I have a PHP configuration file which stores settings like:

 define("SETTING_1", ""); define("SETTING_2", ""); define("SETTING_3", ""); 

By default the settings are empty, but then I have an installer file install.php which basically prompts the user for the settings. What is the best way to open the settings file, find the define for each setting and insert the value? Finally, need to save the settings file to disk.

I want to ovoid using regular expressions or simple string search. I came across token_get_all() , does it makes sense to use it?

token_get_all() isn’t really going to help you, since it’s just going to give you all of the parse token from the string. You’ll spend more time writing a state machine or parser for the result of token_get_all() than you would with the other methods you listed.

The easiest way to achieve this is to use some sort of known, unique placeholder, like this:

define("SETTING_1", ">"); define("SETTING_2", ">"); define("SETTING_3", ">"); 

Then, you open the file, and replace all of those placeholders with their actual values using str_replace() :

$file = file_get_contents( "config.php"); $config = str_replace( array( '>', '>', '>'), array( $setting1, $setting2, $setting3), $file ); file_put_contents( 'config.php', $config); 

Is the define() way definite?

If you really NEED to have it in PHP format, use var_export() and include, which is on the other hand totally insecure. or use INI files, i.e. create ini file, write values in PHP

Читайте также:  Css class onclick event

Don’t use Constants for user settings. It is a bad idea. Create an array and store in a file with var_export with second parameter ‘true’. It is readable and can be regenerated by using array functions.

Creating configuration file in php, You can create an INI configuration file for your project and the parse it using php parse_ini_file function.

What is the difference between Loaded Configuration File displayed in «phpinfo()» and the other diplayed with command «php —ini»

I use wamp server and windows 10 OS, When i see the phpinfo() information, it shows Loaded Configuration File Which is different than Loaded Configuration File displayed when i run php —ini command.

phpinfo() :

php —ini

It’s normal to have loaded files are not the same?

And how can i change my Loaded Configuration File?

php —ini is the cli version of php.ini

phpinfo is the php.ini from the webserver

there is a reason that these are separated. Most of the times you want more privileges on one or the other.

to change the version, left click (1 time) system tray wamp icon > php >version And select your wanted version. It might give errors but if you really need you might copy one php.ini to the other, but i really advice against this.

PHP 7 — PHP.INI File Configuration, The PHP configuration file, php.ini, is the final and immediate way to affect PHP’s functionality. The php.ini file is read each time PHP is initialized.

Источник

Production-friendly Configuration Files in PHP

In config.php , first we include the correct config file according to the server name. Make sure you replace the domains with your production and development domains in the following code. Then, we can include the default configurations file.

 if ($_SERVER['SERVER_NAME'] === 'production.com')  include_once 'config.server.php'; > else if ($_SERVER['SERVER_NAME'] === 'localdomain.com')  include_once 'config.dev.php'; > include_once 'config.default.php'; 

Each config file can contain code as following.

// database config define('DB_HOST', 'localhost'); define('DB_USERNAME', 'root'); // . // domain config define('PROTOCOL', 'https://'); // . // and any thing 

The default config file can contain configurations which are common to both production and development.

define('MAX_USERNAME_LENGTH', 15); // etc. 

Now, in any other PHP file where you need configurations to be included, you can just include the config.php file.

 include_once '/path/to/config.php'; 

You may already have used this technique previously. But, if you haven’t I’m happy you learned something new. I think this is the best way to save configs as there’s no need to rename files in production server each time you upload, which makes no sense. (I have seen some people do that)

Источник

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