unable to set environment variable in PHP

半腔热情 提交于 2021-01-28 14:09:09

问题


here is the code:

putenv("LC_ALL=ru_RU.utf8");
print_r($_ENV) . PHP_EOL;
echo getenv('LC_ALL') . PHP_EOL;

I get following response:

Array (
[USER] => www-data
[HOME] => /var/www
[FCGI_ROLE] => RESPONDER
[QUERY_STRING] =>
[REQUEST_METHOD] => GET
[CONTENT_TYPE] =>
[CONTENT_LENGTH] =>
[SCRIPT_FILENAME] => /var/www/cms/public/index.php
[SCRIPT_NAME] => /index.php
[REQUEST_URI] => /backend/users
[DOCUMENT_URI] => /index.php
[DOCUMENT_ROOT] => /var/www/cms/public
[SERVER_PROTOCOL] => HTTP/1.1
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_SOFTWARE] => nginx/1.0.2
[REMOTE_ADDR] => 127.0.0.1
[REMOTE_PORT] => 48644
[SERVER_ADDR] => 127.0.0.1
[SERVER_PORT] => 80
[SERVER_NAME] => cmsdev.com
[REDIRECT_STATUS] => 200
[PATH_INFO] => /index.php
[HTTP_HOST] => cmsdev.com
[HTTP_USER_AGENT] => Mozilla/5.0 (
X11; Linux x86_64; rv:5.0
) Gecko/20100101 Firefox/5.0
[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
[HTTP_ACCEPT_LANGUAGE] => ru,en-us;q=0.7,en;q=0.3
[HTTP_ACCEPT_ENCODING] => gzip, deflate
[HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
[HTTP_CONNECTION] => keep-alive
[HTTP_COOKIE] => PHPSESSID=ipsldis425a3pitimet7uehaa7; locale=ru_RU; timezone=%7B%22name%22%3A%22UTC%2B0%22%2C%22offset%22%3A0%7D
[HTTP_CACHE_CONTROL] => max-age=0
)

ru_RU.utf8 

So - it looks like instead of $_GET - it prints $_SERVER and it doesn't contain LC_ALL. Any clues? (variables_order = "EGPCS").


回答1:


It doesn't print $_SERVER, it print $_ENV. Environment variables are set the web server which is why you see then when you print $_ENV. As it says in the $_ENV documentation, it says:

An associative array of variables passed to the current script via the environment method.

These variables are imported into PHP's global namespace from the environment under which the PHP parser is running. Many are provided by the shell under which PHP is running and different systems are likely running different kinds of shells, a definitive list is impossible. Please see your shell's documentation for a list of defined environment variables.

Other environment variables include the CGI variables, placed there regardless of whether PHP is running as a server module or CGI processor.

Those values are set when the script is first executed. When you call setenv(), those values are not automatically added to $_ENV. You either have to add them manually or use the getenv() function.

This was also documented in the putenv documentation comments.




回答2:


Is safe_mode enabled? If so, you may be only able to set certain variables. See this documentation of putenv on php.net.



来源:https://stackoverflow.com/questions/6728483/unable-to-set-environment-variable-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!