0

I have environmental variables set in /etc/bash.bashrc and profile. They work fine in shell scripts, but not php.

I have tried:

$VAR = getenv('VAR');
$VAR = $_ENV["VAR"];

If I run the php script from the command line, they print out ok. But if I run it from web, they remain blank.

I did some research and edited php.ini to have an "E" in the variables order. Also commented out the next line about request_order (since it says leaving blank will default back to value set in variables_order). I restarted and it didn't work.

Solved (partially): I added "SetEnv VAR /dir/dir/dir" to the /etc/apache2/sites-available/default file. However, I still have the problem of stringing together the $VAR and the rest of the dir...that's not working. For example:

$VAR = "/var/www";
$new = "$VAR/dir2/file.txt";

When I go to open the file $new, the php script just hangs (when run from web browser). However, if I run the php script from the command line, the dang thing works! Twilight zone.

Corepuncher
  • 375
  • 2
  • 4
  • 15
  • 1
    Are you running these PHP tests from the Linux cli, or through the web sapi? If the latter, then you need to define the environment variables for your webserver (e.g. Apache envvars file) – Mark Baker Dec 18 '14 at 17:45
  • *You can see a list of all the environmental variables by using phpinfo().* - http://php.net/getenv ; Do they show there? – Hanky Panky Dec 18 '14 at 17:49
  • What version of php are you running? – Demodave Dec 18 '14 at 18:02
  • MARK: AH...if I run from command line, the variables print out, but if I run from web browser, they do not. HANKY: If I do php -i, my variables show up under "Environment", and they also show up under "PHP Variables" in the form _SERVER["VAR"]. DEMODAVE: 5.3.3 – Corepuncher Dec 18 '14 at 18:22
  • Mark I added export VAR=/dir/dir/dir in my envvars file and restarted, but still doesn't print out through web. Also, I was already sourcing an env file at the bottom of envvars, and in that file I had all the variables defined. So again, the variables print out if I run this php script from command line, but not in my web browser. – Corepuncher Dec 18 '14 at 18:34
  • 1
    FINALLY got it to work. I added "SetEnv VAR /dir/dir/dir" to the /etc/apache2/sites-available/default file, and it worked! – Corepuncher Dec 18 '14 at 18:49

0 Answers0