Score:1

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

sa flag

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)

Score:2
fr flag
anx

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.

peter avatar
sa flag
In php 7.4, getenv("HTTP_PROXY", true) still returns nothing. phpinfo() shows mod_php7 in Loaded Modules, which means I'm not using php as CGI?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.