大网数据广告活动
服务器运维问答

Why environment variable set by SetEnv in apache httpd.conf cannot be passed to php?

· 52CE 我爱测

I set an environment variable in httpd.conf: SetEnv http_proxy "http://localhost:3128" But I cannot get this variable in php using getenv : <?php echo getenv("http_proxy"); phpinfo(); print_r($_ENV); getenv returns empty and prin…

问题原文

原帖链接 · peter · CC BY-SA 4.0

I set an environment variable in httpd.conf:

SetEnv http_proxy "http://localhost:3128"

But I cannot get this variable in php using getenv:

<?php
echo getenv("http_proxy");
phpinfo();
print_r($_ENV);

getenv returns empty and print_r($_ENV) shows the following content:

Array ( [LANG] => C [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin [NOTIFY_SOCKET] => /run/systemd/notify ) 

Interestingly, the environment variable is shown under the "Apache Environment" section of the phpinfo output.

(my system is apache 2.4/php 5.6/CentOS7)

已采纳解答

原帖链接 · anx · CC BY-SA 4.0

PHP 5.6 has bee unsupported for 3 years now. Upgrade to a current version now! Then, change the following:

Use SetEnv HTTP_PROXY "http://localhost:3128" - environment variables are case sensitive.

Use getenv(varname: "HTTP_PROXY", local_only: true) to request a variable from the server environment.

If you do not explicitly specify to only receive local variables, you would instead get results from fastcgi, so for a varname starting with HTTP_ that could instead dangerously confuse unsafe code with headers and is silently discarded. Opting out of the SAPI behaviour is documented in the getenv() function description:

If PHP is running in a SAPI such as Fast CGI, this function will always return the value of an environment variable set by the SAPI, even if putenv() has been used to set a local environment variable of the same name. Use the local_only parameter to return the value of locally-set environment variables.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-11-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。