1

I use Azure to deploy my code, so I'm using server's variables to save important settings (like client_id and client_secrets).

$_SERVER['APPSETTING_Imgur_Client_Id'];

Set Azure Application Environment variables

Azure $_SERVER

I would like to use the same mecanism in my development environnement (Windows), by setting Environnement variables (directly to windows) and use them in my app.

So I have defined 2 Windows Variables (system properties->Environment Variables->User Variables):

Set Windows's Evironment variables

But thoses variables aren't available through $_SERVER['APPSETTING_Imgur_Client_Id']; (with php -S [::]:80 index.php cmd). I got only default $_SERVER variables.

So I'm askying:

  • How to read thoses Windows's evironment variables with php?

  • If we can't how to add custom $_SERVER variables (without using the php application code).


I'm using:

PHP 7.1.10
&
Windows Server 2016

I have changed the locations of variables (now under System variables) and restarted my computer.

But after that, even with getenv('APPSETTING_Imgur_Client_Id'') or $_SERVER I can't access to my custom variables.

But with command line php -r 'var_export($_SERVER);' I can see my variables

If I want to edit my code, I clone the repo and launch the webserver from the local directory with php -S [::]:80 index.php. It seam that the php -S command doesn't pass windows environment.

Thanks you.

Edit: Solution:

use getenv see picture below:

Postman sample, with both user env and system env

1
  • @NineBerry Your answer was correct, getenv('somevar') works, not $_SERVER['somevar']. Should I roll back to previous question ? Or should I post an answer ? thanks a lot. Commented Dec 31, 2017 at 3:20

1 Answer 1

1

Use the getenv function.

Documentation at http://php.net/manual/en/function.getenv.php

Note that the variables set under system properties->Environment Variables->User Variables can not be seem by a web server when the web server runs under a different user account. You need to set the variables via system properties->Environment Variables-> System Variables (and then restart the web server to have them available)

Sign up to request clarification or add additional context in comments.

4 Comments

I have changed the locations of variables (now under System variables) and restarted my computer. But after that, even with getenv('APPSETTING_Imgur_Client_Id'') or $_SERVER I can't access to my custom variables. Edit: But with command line php -r 'var_export($_SERVER);' I can see my variables
@user8738918 what exactly is your setup for testing your php code?
If I want to edit my code, I clone the repo and launch the webserver from the local directory with php -S [::]:80 index.php. So I have a php7 and a windows server 2016 (10.0.14393). It seam that the php -S command doesn't pass windows environment.
@user8738918 same problem. seems php loads "user env variables" instead of "system env variables", so when you run php from browser - it loads from user "default" with his own env, in case of you write "php -r" while you run CLI from your current user with your env. -- Still dont know how to load "system variables"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.