PHP getenv() doesn't return all variables, even if they are set and can get directly

Asked
Active3 hr before
Viewed126 times

7 Answers

return
90%

Same issue: getenv() doesn't show all variables, even if they are set,I'm having a problem with PHP getenv() function, when I set a variable with putenv(KEY=VAL) I can fetch it directly with getenv(KEY) but it won't return as part of the array when I call getenv() without any key., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , 1 I'm guessing getenv() is like $_ENV and therefore its return value doesn't change after runtime calls to putenv. But this is very poorly documented in any case. – Jeto Nov 25 '20 at 22:25

I'm having a problem with PHP getenv() function, when I set a variable with putenv(KEY=VAL) I can fetch it directly with getenv(KEY) but it won't return as part of the array when I call getenv() without any key.

< ? php

putenv("FOO=BAR");
var_dump(getenv('FOO'))
var_dump(getenv())
load more v
88%

putenv() - Sets the value of an environment variable,getenv — Gets the value of an environment variable, Gets the value of an environment variable. , You can see a list of all the environmental variables by using phpinfo(). Many of these variables are listed within » RFC 3875, specifically section 4.1, "Request Meta-Variables".

load more v
72%

Looks like this may be a security feature (stopping php from reading environment variables).,First, I tried sourcing the environment variable though an env file like it states on the Sendgrid website. , Community Tools and Integrations ,When I do printenv from my droplet’s terminal, I can see the variable so I imagine it is set on my server but how to do I make it be seen by PHP?

I copied the api call for Sendgrid into my site code and tried to send an email. I received a permission denied error. I contacted the Sendgrid support before making this post and have been working with them to try and fix this issue but we are stuck because it does not seem like PHP is picking up the environment variable that I have set to store my Sendgrid api key.

When I hard code in the key, I am able to send emails through Sendgrid with one issues, it is only when I attempt to have the api key called through an environment variable. Like this: `$apiKey = getenv('SENDGRID_API_KEY');

$apiKey = getenv('SENDGRID_API_KEY');
load more v
65%

When dealing with env vars in a PHP project, you want to make sure your code is going to always get the variable from a reliable source, be it $_ENV, $_SERVER, getenv… But those three methods are not returning the same results!,One of the main issue now that we have type casting in PHP is that our settings coming from env vars are not always properly typed.,I recommend to commit a .env file with values made for the developers: everyone should be able to checkout your project and run it the way they like (command line server, Apache, nginx…) without dealing with configuration.,As this variable is only locally defined, we can’t read it from another terminal (another parent). So the idea is to make sure your application always inherits the needed variables.

When you run a program, it inherits all environment variables from its parent. So if you set a variable named YOLO with the value covfefe in your bash and then run a command, you will be able to read YOLO in any child process.

$ YOLO = covfefe php - r 'echo getenv("YOLO");'
covfefe
load more v
75%

Environment variables from $_ENV, $_SERVER, and getenv are visible in the phpinfo output.,Secondly, it’s very difficult to guarantee that all the C libraries you depend on will avoid getenv. For example, the finfo_open calls getenv if you don’t specify the $magic_file. Even $_SERVER and $_ENV call getenv when they are initialized. You would need to audit all of libc, PHP, every PHP library, and every extension to guarantee getenv wasn’t being called.,The Symfony process component does pass $_SERVER and $_ENV to the child process by default. To prevent that you can explicitly set the environment variables.,The $_SERVER superglobal contain environment variables in addition to anything the web server passes along.

If you are using a Unix operating system like MacOS or Linux you can open up a terminal and see the value of the $HOME environment variable like this:

»
echo $HOME
   /
   Users / matt
load more v
40%

Use variables to take control over your project’s build process and its runtime environment. They are set independently of the project’s code base but are available to your code at build or runtime.,Note that any changes to project variables require you to deploy your environments to have effect.,To access environment variables in your app, check the documentation page for your given language.,Add secrets for all environments in project variables using the management console or the CLI.

platform variable: create--level project--name foo--value bar
load more v
22%

If you want to access all the data parameters you can use getParsedBody():,If you want to access all the query parameters you can use getQueryParams():,In order to switch back to using file upload arrays instead, set the configuration value App.uploadedFilesAsObjects to false, for example in your config/app.php file:,You can use a dot separated names to access nested data. For example:

$controllerName = $this - > request - > getParam('controller');
load more v

Other "return-undefined" queries related to "PHP getenv() doesn't return all variables, even if they are set and can get directly"