|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-03-22 20:52 UTC] matt at mattallan dot me
Description:
------------
PHP version:
PHP 7.3.3 (cli) (built: Mar 8 2019 16:40:07) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.3, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.3, Copyright (c) 1999-2018, by Zend Technologies
(installed with Homebrew)
Relevant ini params:
variables_order = "EGPCS"
auto_globals_jit = Off
Overview:
I set the environment variable `FOO` using the fastcgi_param in my Nginx configuration. If I make a HTTP request to the server I am able to access the environment variable using `getenv()['FOO']` but I am not able to access the environment variable using `getenv('FOO')`.
The same issue occurs with the Caddy server. If I set the environment variable in the fpm configuration using `env[FOO]=bar` the issue does not occur. If I run the script from the CLI or the built in web server it works as expected.
Test script:
---------------
<?php
$env = getenv();
var_dump(array_key_exists('FOO', $env));
var_dump($env['FOO']);
var_dump(getenv('FOO'));
Expected result:
----------------
I expect to see the output:
bool(true)
string(3) "bar"
string(3) "bar"
Actual result:
--------------
I see the output:
bool(true)
string(3) "bar"
bool(false)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 21:00:01 2025 UTC |
To be clear, are you seeing this with 'FOO' or is foo being used as a metasyntatic name?, Also, please could you run and show the output of: $env = getenv(); foreach ($env as $key => $value) { var_dump($key); } $var = 'FOO'; var_dump(getenv($var)); var_dump($env[$var]); var_dump($var); with FOO replaced by the actual name if it isn't FOO.I inspected the exchange with Wireshark and it seems correct. The FCGI spec says: > FCGI_PARAMS is a stream record type used in sending name-value pairs from the Web server to the application. The name-value pairs are sent down the stream one after the other, in no specified order. > FastCGI transmits a name-value pair as the length of the name, followed by the length of the value, followed by the name, followed by the value. Lengths of 127 bytes and less can be encoded in one byte, while longer lengths are always encoded in four bytes: And the CGI 1.1 spec says: > Meta-variables are passed to the script in identically named environment variables. These are accessed by the C library routine getenv() or variable environ. Both Caddy and Nginx are sending `03 03 46 4f 4f 62 61 72` (3 3 FOO BAR), which matches the spec. I put the output in a gist here: https://gist.github.com/matt-allan/e845317b09765f1a4d4cf9b8743c3680 I read through the code and it looks like calling `getenv` without a param eventually calls fcgi_loadenv with cgi_php_load_env_var, but if you pass varname to getenv it only calls fcgi_getenv.