Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting PHP variables in httpd.conf?

Tags:

php

apache

I'd like to automatically change my database connection settings on a per-vhost basis, so that I don't have to edit any PHP code as it moves from staging to live and yet access different databases. This is on a single dedicated server.

So I was wondering, can I set a PHP variable or constant in httpd.conf as part of the vhost definition that the site can then use to point itself to a testing database automatically?

$database = 'live';
if (some staging environment variable is true) {
    $database = 'testing'; // and not live
}

If this isn't possible, I guess in this case I can safely examine the hostname I'm running on to tell, but I'd like something a little less fragile

Hope this makes sense

many thanks

Ian

like image 686
Polsonby Avatar asked Sep 06 '25 11:09

Polsonby


2 Answers

Yep...you can do this:

SetEnv DATABASE_NAME testing

and then in PHP:

$database = $_SERVER["DATABASE_NAME"];

or

$database = getenv("DATABASE_NAME");
like image 60
JW. Avatar answered Sep 09 '25 05:09

JW.


You can set an environment variable and retrieve it with PHP.

In httpd.conf:

SetEnv database testing

In your PHP:

if (getenv('database') == 'testing') {

or

if ($_SERVER['database'] == 'testing') {
like image 40
Christian Lescuyer Avatar answered Sep 09 '25 03:09

Christian Lescuyer