Shai Ben-Dor
Shai Ben-Dor

Reputation: 573

getenv() doesn't work on Apache

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.

Upvotes: 1

Views: 6138

Answers (3)

MegaSpaceHamlet
MegaSpaceHamlet

Reputation: 371

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.

Upvotes: 1

simhumileco
simhumileco

Reputation: 34563

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.

Upvotes: 1

Amit Ray
Amit Ray

Reputation: 3485

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'];

Upvotes: 1

Related Questions