Bug #74143 [Opn->Csd]: putenv can't update existing environment variables, still returns true

From: Date: Fri, 24 Feb 2017 23:15:44 +0000
Subject: Bug #74143 [Opn->Csd]: putenv can't update existing environment variables, still returns true
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-14463@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74143&edit=1

 ID:                 74143
 Updated by:         googleguy@php.net
 Reported by:        greg dot bowler at g105b dot com
 Summary:            putenv can't update existing environment variables,
                     still returns true
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            Documentation problem
 Operating System:   Linux Ubuntu 16.04
 PHP Version:        Irrelevant
-Assigned To:        
+Assigned To:        googleguy
 Block user comment: N
 Private report:     N

 New Comment:

This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

This has been fixed in the latest revision of the documentation.

http://svn.php.net/viewvc?view=revision&revision=341970


Previous Comments:
------------------------------------------------------------------------
[2017-02-24 21:41:52] googleguy@php.net

I'm changing this into a documentation bug since the behavior you describe is actually just
undocumented rather than an actual bug in PHP.

When you using getenv() there is an undocumented second boolean parameter, which when set to true,
will only return the local value. Since PHP's environment and the environment your nginx web
server runs in are isolated by design, getenv() will by default return value set by fastcgi_param
here unless you explicitly ask for the local_only copy.

So fastcgi_param TESTVAR_ENV old-value;

var_dump(getenv("TESTVAR_ENV"));
var_dump(putenv("TESTVAR_ENV=new-value"));
var_dump(getenv("TESTVAR_ENV", true));

Will return new-value as expected.

See source for reference: https://github.com/php/php-src/blob/php-7.1.2/ext/standard/basic_functions.c#L4040

------------------------------------------------------------------------
[2017-02-21 23:08:22] greg dot bowler at g105b dot com

Description:
------------
The putenv function sets the value of an environment variable. According to the manual, putenv
returns true on success, false on failure.

The scope of this bug report tracks setting an existing environment variable to a new value. 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. See test script below for example.

Test script:
---------------
Webserver config file:
----------------------

fastcgi_param   TESTVAR_ENV     old-value;


test.php:
---------

var_dump(getenv("TESTVAR_ENV"));
var_dump(putenv("TESTVAR_ENV=new-value"));
var_dump(getenv("TESTVAR_ENV"));


Output:
-------

string(12) "old-value"
bool(true)
string(12) "old-value"

Expected result:
----------------
One of the two outcomes are expected:

1. putenv returns false, as there is a failure in setting the variable.
2. getenv returns the newly updated value.

Actual result:
--------------
The reason I consider this a bug is due to the manual stating that putenv will return true on
success.

1. putenv is returning true, indicating the variable has been successfully set.
2. getenv is returning the old value, not updated.


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=74143&edit=1


Thread (2 messages)

« previous php.doc.bugs (#14463) next »