0

For some reason, I need to connect database by this:

return array(
    'host'     => $_ENV['host'],
    'database' => $_ENV['dbname'],
);

I tried many things, for axample:

Export in .bashrc

  1. add export host="localhost" in ~/.bahsrc
  2. source ~/.bashrc
  3. echo $host // localhost

echo $_ENV['host']; in php file is empty

Set in php.ini

  1. edit php.ini
  2. add host = "loclhost" at bottom
  3. restart apache

echo $_ENV['host']; in php file is empty

Set in apache conf

  1. add test.conf to conf-available
  2. add SetEnv host localhost
  3. a2enconf test.conf
  4. restart apache

echo $_ENV['host']; in php file is empty, but echo getenv['host']; works, but I need to use $_ENV to get the environment variable.

Chan
  • 1,947
  • 6
  • 25
  • 37
  • 1
    Have a look at your `variables_order` directive in php.ini http://us.php.net/manual/en/ini.core.php#ini.variables-order Your issues is mentioned in the first user comment on the `$_ENV` manual page. http://php.net/manual/en/reserved.variables.environment.php – Michael Berkowski Nov 22 '14 at 03:42
  • See also http://stackoverflow.com/questions/3780866/why-is-my-env-empty – Michael Berkowski Nov 22 '14 at 03:45
  • I indeed saw this, and I change all type of order and restart apache again and again still empty. – Chan Nov 22 '14 at 05:08
  • Ooops, I got it, it can only work on Windows? – Chan Nov 22 '14 at 05:11
  • It should work on platforms besides Windows. I've used it on Linux extensively. Are your environment vars set in `$_SERVER`? Based on info in the other answer, if you use `SetEnv` in Apache, that populates `$_SERVER` rather than `$_ENV`. Variables you set in .bashrc would only be available to Apache if Apache runs as a the same user as your user account, which isn't all that common except on some shared hosting – Michael Berkowski Nov 22 '14 at 12:49

0 Answers0