This article introduces the relevant knowledge about system variables and environment variables in PHP. Friends in need can refer to it.
In daily PHP programming, you sometimes encounter modifications to system variables and environment variables to adapt to simple and efficient PHP development. In a virtual host environment, sometimes it is necessary to set the PHP environment variable value through the PHP environment variable operation function. This article introduces how to set the PHP environment variable $_SERVER and PHP system constants for your reference. PHP provides many default system variables for obtaining system configuration information, network request-related information, etc. The default system variables and their functions are as follows: Variable function $GLOBALS[] stores all global variables in the current script, where KEY is the variable name and VALUE is the variable value. $_SERVER[] Current WEB server variable array $_GET[] stores the data in the form submitted with the GET method $_POST[] stores the data in the form submitted using the POST method $_COOKIE[] Gets or sets the variable array stored in the user's browser cookies $_FILES[] stores the data submitted by uploaded files to the current script $_ENV[] stores the current WEB environment variables $_REQUEST[] stores an array of all requests in the submitted form, including everything in $_GET, $_POST, $_COOKIE, and $_SESSION $_SESSION[] stores the session variable array of the current scriptDue to different location files, the content displayed in different environments may be different. Like system variables, PHP also provides some default system constants for use. These system constants can be applied at any time in the program, but we cannot change the values of these constants arbitrarily. Some default system constants commonly used in PHP and their functions are shown in Table 2-2 Show. Constant function __FILE__ stores the absolute path and file name of the current script __LINE__ stores the line number where the constant is located __FUNCTION__ The name of the function where the constant is stored __CLASS__ is the name of the class in which the constant is stored. PHP_VERSION stores the current PHP version number PHP_OS stores the operating system of the current server$_GET and $_POST are mainly for data submitted by FORM forms, $_COOKIE and $_SESSION are mainly for client browser and server-side session data. $_FILES is mainly for the data submitted when uploading files, $_REQUEST is mainly for the submission form All request arrays in the list, including all contents in $_GET, $_POST, and $_COOKIE, you can use the print_r function to output $_REQUEST or $_COOKIE respectively for comparison. Introduction to PHP environment variable $_SERVER It is a PHP global environment variable that contains server-side related information. $HTTP_SERVER_VARS is used in versions before PHP4.1.0. $_SERVER['PHP_SELF'] The file name of the currently executing script, related to the document root. In the FORM form, if the executable file is itself, you can use $_SERVER['PHP_SELF'] in ACTION. The advantage is that when the executable file name You don't need to frequently replace the file name in ACTION when there are changes.
$_SERVER['SERVER_NAME'] The name of the server host where the currently running PHP program is located. $_SERVER['REQUEST_METHOD'] The request method when accessing the page, namely GET, HEAD, POST, PUT. $_SERVER['DOCUMENT_ROOT'] The document root directory where the currently running PHP program is located. That is the definition in the PHP.INI file. $_SERVER['HTTP_REFERER'] links to the URL address of the previous page of the current page. Very useful in page jump function. $_SERVER['REMOTE_ADDR'] The IP address of the visitor who is browsing the current page. $_SERVER['REMOTE_HOST'] The host name of the user who is browsing the current page. $_SERVER['REMOTE_PORT'] The port used by the browsing user to connect to the server. $_SERVER['SCRIPT_FILENAME'] The absolute path name of the currently executing script. $_SERVER['SERVER_PORT'] The port used by the server $_SERVER['SCRIPT_NAME'] contains the path of the current script. This is useful when the page needs to point to itself. $_SERVER['REQUEST_URI'] The URI required to access this page. Such as "/index.html". $_SERVER['PHP_AUTH_USER'] is used in the HTTP user login authentication function. This variable is the user name entered by the user. $_SERVER['PHP_AUTH_PW'] is used in the HTTP user login authentication function. This variable is the password entered by the user. $_SERVER['AUTH_TYPE'] is used in the HTTP user login authentication function. This variable is the authentication type. Note: The above-mentioned PHP global environment variables, when register_globals in php.ini is set to on, these variables are available in all PHP program scripts, that is, the $_SERVER array is separated. Of course, for safety reasons, It is better not to turn register_globals on. PHP system constants __FILE__ The absolute path and file name of the current PHP program script __LINE__ stores the line number where the constant is located __FUNCTION__ stores the function name where the constant is located __CLASS__ The name of the class in which the constant is stored PHP_VERSION stores the current PHP version number, which can also be obtained through the PHPVERSION() function. PHP_OS stores the operating system of the current serverFor more information about the PHP environment variable $_SERVER, please refer to the PHP manual. In addition, in a virtual host environment, the PHP environment variable value needs to be set through the PHP environment variable operation function. Ini_set and ini_get are mainly used. There are more such functions, such as error report settings in PHP, etc., which are actually involved. For relevant content in PHP.INI, you can find some documents for reference. |