1

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)

1 Answer 1

2
+50

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.

2
  • 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?
    – peter
    Commented Nov 21, 2022 at 15:07
  • @peter Did you ever figure out what the issue is? I have the same problem that SetEnv variables are not passed to PHP 7.4 CGI scripts.
    – Casper
    Commented Jul 3 at 19:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .