Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is getenv() only returning a variable when run from the terminal, not via web server

Tags:

php

I'm trying to use a custom environment variable for environment detection in my PHP application. I've set the variable system-wide in /etc/environment, rebooted, and checked that it is visible through the terminal using printenv.

I've also checked that it is visible when running PHP at the command line:

php -r 'echo getenv("LOCAL_DEVELOPMENT");'

//returns 'true' as expected

But when I try from a web page script, the variable is not found:

var_dump(getenv("LOCAL_DEVELOPMENT"));

//returns 'boolean false' (not expected)

I'm using Ubuntu 12.04 LTS with PHP 5.4 and Apache 2.2.

Why is the variable not visible from a web script, and how do I fix this?

like image 482
mtmacdonald Avatar asked May 28 '14 14:05

mtmacdonald


1 Answers

Assuming your using apache server, I think its down to Apache being restricted from the System Environment variables.. for security.

You should set env variables on apache like SetEnv LOCAL_DEVELOPMENT true or even better SetEnv APPLICATION_ENV development is generally considered the standard.

like image 170
RaggaMuffin-420 Avatar answered Oct 17 '22 16:10

RaggaMuffin-420