I'm trying to determine the best way of having a PHP script determine which server the script/site is currently running on.
At the moment I have a switch()
that uses $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT']
to determine which server it's on. It then sets a few paths, db connection parameters, SMTP paramters and debug settings based on which server it's on. (There maybe additional parameters depending on the site needs.)
This means that I can simply drop the site onto any of the configured servers without having to change any code (specifically the configuration). If it's a new server, then I simply add a new case
and it's ready from then on.
We have done loading config files based on the same SERVER_NAME:SERVER_PORT
combination, but found that it's another file you have to maintain, plus we weren't sure on the speed of parsing ini files, (although having extra cases for each server may be just as slow).
Another problem we have is when a site is often moved between 2 servers, but we use the same SERVER_NAME
and SERVER_PORT
on each. This means we need to temporarily comment one case and ensure it doesn't get into the repo.
Another other ideas? It needs to be available on all servers (sometimes SERVER_NAME
and SERVER_PORT
are not). It would also be nice if it worked with the CLI PHP.