无法在PHP中设置环境变量 [英] unable to set environment variable in PHP

查看:72
本文介绍了无法在PHP中设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是代码:

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

我得到以下答复:

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 

所以-看起来好像不是$ _GET $-而是打印$ _SERVER ,它不包含 LC_ALL .有什么线索吗?( variables_order ="EGPCS" ).

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

推荐答案

它不打印 $ _ SERVER ,而是打印 $ _ ENV .环境变量设置为Web服务器,这就是为什么您在打印 $ _ ENV 时看到的原因.正如 $ _ ENV 文档中所说的,它说:

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.

这些变量从导入到PHP的全局名称空间.运行PHP解析器的环境.提供了许多由运行PHP的外壳和不同的系统组成可能运行不同种类的外壳,一个明确的清单是不可能的.请参阅您的Shell文档以获取以下内容的列表:定义的环境变量.

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.

其他环境变量包括放置在此处的CGI变量不管PHP是作为服务器模块还是CGI运行处理器.

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

这些值是在首次执行脚本时设置的.当您调用 setenv()时,这些值不会自动添加到 $ _ ENV 中.您要么手动添加它们,要么使用 getenv()函数.

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.

putenv 中也对此进行了记录文档注释.

This was also documented in the putenv documentation comments.

这篇关于无法在PHP中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆