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
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
Surround Variables:
echo "Вы пришли из: ".getenv("HTTP_REFERER")."
"; $ 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.
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.