Edit: as far as I can tell, my question is because of a defect in PHP. I've copied this question to the PHP bug tracker here: https://bugs.php.net/bug.php?id=74143 and plan to try and implement a fix.
The putenv function sets the value of an environment variable. According to the manual, putenv returns true on success, false on failure.
However, I'm finding that the putenv function sometimes returns true without updating the environment variable for the current session.
To reproduce this issue, set an environment variable in a webserver using PHP FPM, by using the fastcgi_param directive. This is incredibly useful, as it allows setting environment variables in isolation to other hosts on the same server.
Example nginx.conf:
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param TESTVAR_ENV old-value;
include fastcgi_params;
}
Example test.php:
var_dump(getenv("TESTVAR_ENV"));
var_dump(putenv("TESTVAR_ENV=new-value"));
var_dump(getenv("TESTVAR_ENV"));
Output of test.php:
string(12) "old-value"
bool(true)
string(12) "old-value"
As you can see:
- the existing value is read by getenv successfully,
- the putenv function returns true, indicating success,
- the new value is not actually set, which is incredibly confusing.
Am I misunderstanding what the purpose of the putenv function is? Is there some missing documentation on the setenv manual page? How do I use putenv() to update existing environment variable?
putenv("TESTVAR_ENV")
should clear the value, or maybe try using$_SERVER
instead? – Versatile$_SERVER
works as expected. – VersatileReturns TRUE on success or FALSE on failure.
I can't convince myself that "failing" to change the value of an existing var is anything other than a failure... making this a php defect. – Condition