5

I have an environmental variable defined in Windows environmental variables -> user variables

$MY_VARIABLE=mypath

and in php I tried to access it like below

<?php
echo $_ENV["MY_VARIABLE"];

but it doesn't print anything. Am I missing something here or is it that PHP doesn't have access to user environmental variables ?

EDIT: I am running PHP as an Apache module (important info that I left out initially).

Krishnaraj
  • 2,360
  • 1
  • 32
  • 55
  • 1
    you have however, if you *just* added it reboot first (or make yourself comfortable with what environment variables are and what their scope is). Also what does `var_dump($_ENV);` say after reboot? – hakre Oct 06 '12 at 13:54
  • Oh! var_dump($_ENV) itself is empty! – Krishnaraj Oct 06 '12 at 14:00
  • what is executing the script; in which environment does it run? – hakre Oct 06 '12 at 14:00
  • hmmm.. this could be the reason - http://stackoverflow.com/questions/3780866/why-is-my-env-empty – Krishnaraj Oct 06 '12 at 14:06

3 Answers3

6

You $_ENV is empty because of variables_order settings in PHP

Edit your PHP.INI

Change

  variables_order = "GPCS"

To

 variables_order = "EGPCS"

It should work by now if it does not try using .htaccess to achieve this

Add the following to your .htaccess

SetEnv foo bar

You can get it via PHP

var_dump(getenv('foo'),$_ENV);
Baba
  • 94,024
  • 28
  • 166
  • 217
  • Ya, tried that but it still doesn't show up in the $_ENV. The answer in my above comment explains it well. – Krishnaraj Oct 06 '12 at 14:31
  • Try using `putenv` and don't forget to restart your server after change – Baba Oct 06 '12 at 14:32
  • That could work but then that would be available to only one php site, I have multiple sites running on this system that need to access this path. – Krishnaraj Oct 06 '12 at 17:03
  • If I have two websites like /var/www/website1 and /var/www/website2, my htaccess should be in /var/www/.htaccess for the all to sites to use this right ? – Krishnaraj Oct 06 '12 at 17:19
  • Yes, it is a Virtual Host setup. – Krishnaraj Oct 06 '12 at 19:53
  • Not sure if the global .htaccess would work but you can give it a try add `SetEnv foo bar` to it ..... and use `var_dump(getenv('foo'));` to retrieve it – Baba Oct 06 '12 at 19:56
1

Recently i wrote a library to get values from environment variables and parse to the PHP data types. This library can be used to parse environment variables to PHP data types (like the casting to integer, float, null, boolean), parse the complex data structures like a JSON string and more with the contribution of the commnunity.

The library is available here: https://github.com/jpcercal/environment

And to get the values from environment variable to do it:

<?php
// ...
require "vendor/autoload.php";
// ...
var_dump(Cekurte\Environment\Environment::get("MY_VARIABLE"));

Enjoy it.

João Paulo Cercal
  • 733
  • 1
  • 8
  • 12
0

As suggested by user Baba, do this - variables_order = "EGPCS" in the relevant php.ini file.

Then in the "Environment Variables" (Control -> Advanced system settings) add a new environment variable key value. Then restart the server. Use the following to check if the CONSTANT exists-

defined('YOUR_ENV_VAR') || define('YOUR_ENV_VAR', (getenv('YOUR_ENV_VAR') ? getenv('YOUR_ENV_VAR') : 'put_your_default_value_here'));

Use this new environment variable values as the value of a CONSTANT in your code. So, if the Environment Variable is set to -

C:/foo ... in Environment Variables

The code below will pick up this value.

defined('MY_VARIABLE') || define('MY_VARIABLE', (getenv('MY_VARIABLE') ? getenv('MY_VARIABLE') : 'C:/bar'));

However, if the EV is not there then the default value (C:/bar) will be used.

You can use the above to dynamically define your PRODUCTION and DEVELOPMENT environments without the need to change the code.

PiggyMacPigPig
  • 345
  • 4
  • 10