Menu
Is free
check in
home  /  Tips / PHP environment variables. All you need to know about environment variables in php translation

PHP environment variables. All you need to know about environment variables in php translation

Attention! The article is written under Windows 7, but fundamental differences in the addition of PHP in environment Variables In other versions (Window XP, Windows Vista, Windows 8, ...) No.

Information! All paths in the settings will be indicated on the basis of the fact that Denwer is installed in the D: / Web folder. When using data from the article, do not forget to change this path to your.

For convenient use php. In the Windows console, you need to configure environment variables. Otherwise, when working with PHP through the console, instead of the PHP command, you will need to write the full path to the php.exe file.

Suppose that Denwer is installed in the D: / Web folder.

With the right mouse button on the computer icon, properties:

Additional system parameters:

The "Advanced" tab, the "Wednesday parameters" button:

Group " System parameters"Select the PATH variable and click the Edit button:

Add a string D: \\ Web \\ usr \\ local \\ php5; and click "OK":

Restart Denwer. Now you can open the console (Win + R and enter CMD) and check pHP work Enter the command:

To correct these errors, open the D: /Web/usr/Local/PHP5/PHP.ini file. Find variables extension_dir, zend_extension, session.save_path and set the following values \u200b\u200bfor them.

Variable environment

Environment Variables in PHP

Immediately before starting the scenario, the server transmits him some environment variables with information. In certain variables, contain some headlines, but not all (it is impossible to get all the headers). Next, I will give a list of the most important environment variables.

Http_accept

In this variable listed all MIME Types of Datawhich can be perceived by the browser. Row * / * means that the browser understands any type.

Http_accept \u003d image / gif, image / x-xbitmap, image / jpeg, image / pjpeg, application / x-shockwave-flash, application / vnd.ms-excel, application / msword, * / *

Http_referer

This variable represents information about the page with which the user came to this. This variable can be used, for example, to track the user movement according to your site, and then view the most popular routes.

Http_referer \u003d http://www.spravkaweb.ru/php/pril/

Http_cookie.

All cookies are stored in this variable in the URL encoding.

Http_cookie \u003d hotlog \u003d 1; Zdedebuggerpresent \u003d php, phtml, php3; b \u003d b; PHPSESSID \u003d 16805922A9258CDA274316E60F649CF8.

Http_user_agent

Identifies the user's browser. To establish a browser type you need to check this string for the presence of words: if the browser is Internet Explorer.The MSIE substring will be present, and if only the word Mozilla is available, then it is Netscape.

For example:

Http_user_agent \u003d Mozilla / 4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; myie2; Maxthon)

Http_host.

Contains domain name Web server on which the script starts. This variable is sufficiently convenient to use, for example, to generate the full path that is required in the Location header to not be attached to a specific server.

Http_host \u003d www.spravkaweb.ru.

Http_from.

The address email A user who sent a request.

Server_Name.

Domain name or server IP address.

Server_Name \u003d www.spravkaweb.ru.

Server_Software.

The name and version of the server program corresponding to the client's request.

Server_Port.

This variable contains the server port to which the user browser addressed. This is usually 80. The variable can also be used to generate the Location header parameter.

Server_protocol

The variable contains the name and version of the information protocol that was used to request.

Server_protocol \u003d http / 1.1

Remote_addr.

This variable contains an IP address (or domain name) of the user node on which the browser has been launched.

Remote_Port.

The port that is secured by the user's browser to receive server response.

Remote_user.

Identification user name sending a request.

Script_name.

Contains the name of the file containing this script. This variable is convenient to use when the Location header is generated when redirecting (Self-Redirect), as well as to substitute the value of the Action Tag Attribute

On the page that the script calls when starting without parameters (in order not to be attached to a specific scenario name).

Document_ROot.

Root catalog of the WEB document tree.

Request_Method

The method that the user applies during data transmission. It should be noted that a competent scenario must determine on the basis of this variable, which method will use the user and receive data from the corresponding source, and not to calculate that the transfer will be carried out, for example, only by the POST method.

Query_String.

Contains the parameters that in the URL are indicated after the question mark. Recall that they are available both when the GET method and the POST method (if in the latter case they were defined in attribute Action. Tag ).

Path_info.

Contains additional information About the way.

Path_translated

The same information as in the variable Path_info. With the prefix setting the path to the root catalog of the WEB document tree.

Content_Type.

Media type of request data.

Content_Length.

Returns the number of bytes of data sent by the user. This variable must be analyzed if you are engaged in the reception and processing of the post-form.

Gateway_Interface.

The CGI version that the server uses.

Example of using environment variables

Variables

In the RNR variables begin with a dollar sign ( $ ). This sign can follow any number of alphanumeric characters and underscore characters, but the first character can not be a digit or underlining. It should also be remembered that the names of variables in the RNR are sensitive to the register, unlike keywords.

When declaring variables in PNP, it is not necessary to explicitly specify the type of variable, while the same variable can have different types during the program.

The variable is initialized at the time of assigning the value and exists until the program is executed. Those., In the case of a Web page, it means that until the request is completed.

External variables

After the client's request is analyzed by a web server and transferred to the RNR machine, the latter sets a number of variables that contain data relating to the query and are available all the time it is executed. First RNR takes variable environment Your system and creates variables with the same names and values \u200b\u200bsurrounded by the RNR script in order for the scenarios located on the server, the following client system features. These variables are placed in associative array $ Http_env_vars (More about arrays can be found in chapter 4).

Naturally, variable array $ Http_env_vars are systemically dependent (since it is actually variable environment). View the values \u200b\u200bof environment variables for your machine you can using the ENV (UNIX) or SET command.

The RNR then creates a group of get variables that are created when analyzing the query string. The query string is stored in a variable $ Query_String. and represents the information following the symbol " ? "In the requested URL. RNR breaks the row of the query by symbols & on the separate elementsAnd then searches for "\u003d" sign in each of these elements. If the "\u003d" sign is found, then a variable is created with the name from the symbols of the left of the equality sign. Consider the following form:

Action \u003d. "http: //localhost/php/test.php" Method \u003d " get."\u003e HDD: Type \u003d " tEXT."Name \u003d" HDD."/>
CDROM: Type \u003d " tEXT."Name \u003d" CDROM."/>
Type \u003d " submit"/>

If you are in this form in the HDD line, click, for example, "MAXTOR", and in the CDROM string "NEC", then it will generate the following request form:

http: //localhost/php/test.php? HDD \u003d MAXTOR & CDROM \u003d NEC

In our case, PNP will create the following variables: $ HDD. \u003d "MAXTOR" and $ CDROM. \u003d "NEC".

You can work with these variables from your script (from us - Test.php) as with conventional variables. In our case, they are simply displayed:

echo.("

HDD IS $ HDD

"); echo.("

CDROM IS $ CDROM

"); ?>

If the page's request is performed using the POST method, a group of post variables appears, which are also interpreted and placed in an array. $ Http_post_vars.

Immediately before starting the scenario, the server transmits him some environment variables with information. In certain variables, contain some headlines, but not all (it is impossible to get all the headers). Next, I will give a list of the most important environment variables.

Http_accept

In this variable listed all MIME Types of Datawhich can be perceived by the browser. Row * / * means that the browser understands any type.

Http_accept \u003d image / gif, image / x-xbitmap, image / jpeg, image / pjpeg, application / x-shockwave-flash, application / vnd.ms-excel, application / msword, * / *

Http_referer

This variable represents information about the page with which the user came to this. This variable can be used, for example, to track the user movement according to your site, and then view the most popular routes.

Http_referer \u003d http: //www.Syt/PHP/pril/

Http_cookie.

All cookies are stored in this variable in the URL encoding.

Http_cookie \u003d hotlog \u003d 1; Zdedebuggerpresent \u003d php, phtml, php3; b \u003d b; Phpsessid \u003d.

Http_user_agent

Identifies the user's browser. To establish a browser type you need to check this string for the presence of words: if the browser is Internet Explorer, then the MSIE substrate will be present, and if only the word Mozilla is available, it is Netscape.

For example:

Http_user_agent \u003d Mozilla / 4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; myie2; Maxthon)

Http_host.

Contains the domain name of the Web server on which the script starts. This variable is sufficiently convenient to use, for example, to generate the full path that is required in the Location header to not be attached to a specific server.

Http_host \u003d www.Syt.

Http_from.

Email address by sending a request.

Server_Name.

Domain name or server IP address.

Server_name \u003d www.Syt.

Server_Software.

The name and version of the server program corresponding to the client's request.

Server_Port.

This variable contains the server port to which the user browser addressed. This is usually 80. The variable can also be used to generate the Location header parameter.

Server_Port \u003d 80.

Server_protocol

The variable contains the name and version of the information protocol that was used to request.

Server_protocol \u003d http / 1.1

Remote_addr.

This variable contains an IP address (or domain name) of the user node on which the browser has been launched.

Remote_Port.

The port that is secured by the user's browser to receive server response.

Remote_user.

Identification user name sending a request.

Script_name.

Contains the name of the file containing this script. This variable is convenient to use when the Location header is generated when redirecting (Self-Redirect), as well as to substitute the value of the Action Tag Attribute On the page that the script calls when starting without parameters (in order not to be attached to a specific scenario name).

Document_ROot.

Root catalog of the WEB document tree.

Request_Method

The method that the user applies during data transmission. It should be noted that a competent scenario must determine on the basis of this variable, which method will use the user and receive data from the corresponding source, and not to calculate that the transfer will be carried out, for example, only by the POST method.

Query_String.

Contains the parameters that in the URL are indicated after the question mark. Recall that they are available both with the GET method and when POST method (if in the latter case, they were defined in the Action Tag Attribute ).

Path_info.

Contains additional information about the path.

Path_translated

The same information as in the variable Path_info. With the prefix setting the path to the root catalog of the WEB document tree.

Content_Type.

Media type of request data.

Content_Length.

Returns the number of bytes of data sent by the user. This variable must be analyzed if you are engaged in the reception and processing of the post-form.

Gateway_Interface.

The CGI version that the server uses.

Example of using environment variables

Variable environment

Surround Variables:


"; $ br \u003d getenv (" http_user_agent "); if (STRPOS ($ br," msie ")! \u003d\u003d False) Echo" You have Internet Explorer
"; Else Echo" You have Netscape or others.
"; ECHO" Your IP: ".GeTenv (" Remote_addr ")."
"; ECHO" Here are the parameters in the browser line: ".getenv (" query_string ");?\u003e

This is a short How-to to implement the PHP service configuration, depending on the environment in which it is running. I will be glad if someone tells a more elegant solution or corrects in trifles.

Main idea

Run service, microservices and affiliates within one ecosystem configurable using environment variables.
Problem
In this article, "environment variables" is repeated too many times.
From the PHP-FPM box ignores global environment variables (GETENV FUNCTION), while PHP CLI can receive them.
Prehistory.
This section can be skipped if you have already worked with S.NV

AT this moment I am working on a project written in ZF2. Config files for different environments were used to configure the project. This generates a large number of duplicates of the configuration in the project repository of approximately this type:
  • session.global.php.
  • session.local.php.dist.
  • session.unittest.php.dist.
  • db.global.php.
  • db.local.php.dist.
  • db.unittest.php.dist.
These duplicates have to constantly synchronize each other. In addition, they store a certain PHP logic within themselves, which generates duplication of the code.

So, the project now takes into account the environment, but ...

While the development was carried out on working machines, the project read. Thenv file and everything worked. But when I unfolded the test environment, it turned out that if you ask permanent systemic environment variables, PHP-FPM ignores them. Various recipes from Google and Stackoverflow were reduced to one or another automation of the use of two known methods:

1. Transfer of variables via Nginx Parameter FastCGI_PARAM SomeEnv Test;
2. Installation of variables in the Env format in the PHP-FPM workflow pool configuration.

Both first and second option are convenient for some special situations. But if you think in the paradigm "Configure Wednesday, not an application", then such methods turn out to be much more difficult than for example, just put the.env file into a folder with the project. But after all, the orchestrator, CI system or just system Administrator I should not know the details of the project implementation, this is not elegant.

The proposed solution solution
Combining various recipes from the network, I sold the following working solution.
Tested under CentOS 7, PHP 5.6.14.

1. Open /etc/php.ini - Replace VARIABLES_Order \u003d "GPCS" on Variables_Order \u003d "EGPCS" # After that, PHP will add to the global space environment variables # http://php.net/manual/eni.core.php # INI.VARIABLES-ORDER 2. Open /etc/php-fpm.d/www.conf, not to be confused with /etc/php-fpm.conf (in different systems Maybe in a different place, this is a config of the WWW-pool of processes for PHP-FPM. - Add (or replace, if there is a sudden): clear_env \u003d no # Turn off the cleaning of global variables for launched warkers 3. Set the necessary environment variables to / etc / environment (standard syntax A \u003d b) 4. LN -FS / ETC / ENVIRONMENT / ETC / SYSCONFIG / PHP-FPM # Now Config Enchanting Variables The PHP-FPM service will be just a link to the global config 5. SystemCTL Daemon-Reload && Service PHP-FPM Restart

The same approach with Simlink, in theory, applicable to other services.

Pros of the proposed solution:
- Variables stored in / etc / environment available multiple applications. You can call an echo $ mysql_host in Shell or Getenv ("MySQL_HOST") in PHP.
- Variable environments that are clearly not specified in / etc / environment, will not fall into PHP-FPM. This allows using an orchestrator to monitor the surroundings of the insulated system in which the service is running.

Minuses:
- Unfortunately, PHP-FPM, I did not find a working command for RELOAD by analogy with NGINX, so in case of change / ETC / Environment, you must do systemCTL Daemon-Reload && Service PHP-FPM Restart.

Important: If your application does not work in an isolated environment (server, virtual, container), the definition of environment variables may unpredictably affect the adjacent services in the system due to the coincidences of the names in the global space.