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
Yep...you can do this:
SetEnv DATABASE_NAME testing
and then in PHP:
$database = $_SERVER["DATABASE_NAME"];
or
$database = getenv("DATABASE_NAME");
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') {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With