0

I set an Environment Variable on Windows (System Propeties->Advanced->Environment Variables) and tried to access it from a php code, like this:

getenv('ENV');

My php runs on Xampp's Apache server and it returned false.

I read that for security reasons I have to use PassEnv Directive inorder for Apache to recognize Environment Variables. I tried to use it within .htaccess and .conf files but didn't succeed.

Would like to understand what I am missing, and what should I do to access the environment variable.

  • PHP 5.6
  • Apache 2.0
Shai Ben-Dor
  • 533
  • 9
  • 21

3 Answers3

1

You have to restart your PC for the new environment to be read by PHP. This is the way I did. I set an environment variable through (System Propeties->Advanced->Environment Variables) and then checked it returned bool(false). I restarted my PC and checked again and now it worked. So I suggest you to restart and check again.

EDIT:

Also if you delete the windows environment variable and check the variable in PHP without restarting; it will still show the value. That means the environment variables is stored temporarily until PC restarts. I dont have much idea on windows API but this is the way it works.

I got two more useful links that may help you

  1. Setting environment variables requires reboot on 64-bit
  2. https://serverfault.com/questions/8855/how-do-you-add-a-windows-environment-variable-without-rebooting

EDIT:

Also there is another solution that I found in php.net changing variables order in your php.ini file [variables_order = "GPCS"] to [variables_order = "EGPCS"] and restarting APACHE. EGPCS is (Environment, Get, Post, Cookie, and Server) variable parsing. This will create the $_ENV variable. Try to access the environment variable through $_ENV['ENV'];

Community
  • 1
  • 1
Amit Ray
  • 3,445
  • 2
  • 19
  • 35
  • The environment variable isn't new, thus rebooting didn't solve the problem. I still think this has something to do with PassEnv or any other method of recognizing OS variables within Apache – Shai Ben-Dor Jun 27 '16 at 08:47
  • @ShaiBen-Dor I have added one more method. Just check if that works for you. – Amit Ray Jun 27 '16 at 09:06
1

Try in php.ini file set the variable variables_order to "EGPCS" instead of a developer and production value "GPCS" and restart the server process.

simhumileco
  • 31,877
  • 16
  • 137
  • 115
0

How to get your code to read the updated environment variables:

  1. Stop the Apache Server Process
  2. Close the Xampp Control Panel Window

Then you can re-open the window and restart the process.

I had the same issue as you, but when I did this, it worked. As long as the window is open, Apache has not registered the new Environment Variable. When a new variable is set, Windows sends a message to all programs, so that the next time they open, they will have the new variable registered. Any windows that are open will not register the change.

Also make sure that after you have changed/set the variable to select 'OK' if done through the Control Panel.