Write down your apache environment variables and read them into PHP script

I have an index.php file running some script executing mysql queries. For the obvious security reason I would like to remove the mysql credentials (host, password, user, database) from this script and replace them with some apache environment variables.

I tried to create variables like this in / etc / apache 2 / envvars using the following line of code: export MYSQL_USER='my_user'

and then I intend to return it using the getEnv php function like this: getenv('MYSQL_USER')

but that returns nothing.

Any idea? Thank!

+1


source to share


2 answers


Four steps:



  • in / etc / apache 2 / envvars: export MYVAR='value'

  • in / etc / apache 2 / apache2.conf: PassEnv MYVAR

  • Restart apache
  • in the php file where apache2 resides: echo $_SERVER['MYVAR'];

+2


source


In a virtual host for Apache you can use

SetEnv VARIABLE_NAME value

      

and get the value of that variable from your PHP code using



$variable = getenv('VARIABLE_NAME');

      

You can read more about SetEnv here http://httpd.apache.org/docs/2.2/mod/mod_env.html#setenv

0


source







All Articles