All right, this isn’t exactly a rocket science, it’s just something I use and find very handy.
We all develop (and test) on one computer and deploy to a different computer to make the changes live. Having to run the application in two different environments brings a challenge of using different configuration based on where the application runs.
Multiple configuration files
I started by having two configuration files, one for my dev box and another for the production server. They were named the same and I had one on my dev box and one on the server. Each deployment though, meant that I had to be careful not to overwrite the server file with my local copy. That ruled out using a simple synchronization script like lftp or WinScp.
The simplest solution is to name the configuration files differently and upload them both to both servers. All right, even simpler is to have a single configuration php file and define different values using if/else but that can quickly get out of hands as the number of variables grows.
If you need to upload to more production servers (more than one customer) you need to use some more sophisticated deployment script (Ant?).
Let’s return to the simple solution for now. You need to find a piece of information that can be used as a computer identifier and use it to choose the right set of configuration data.
$_SERVER[‘SERVER_NAME’] & $_SERVER[‘SERVER_ADDR’]
You can use these two for most situations. I develop on my workstation and I have set up domain name based web hosts (C:\xampp\apache\conf\extra\httpd-vhosts.conf) using fake domains (C:\WINDOWS\system32\drivers\etc\hosts).
Using $_SERVER[‘SERVER_NAME’] works when you know it up front but sometimes your client is going to choose it just before launching the site. Or he decides to change it later on. Not a big deal but I don’t like chaos.
$_SERVER[‘SERVER_ADDR’] is also fine but don’t forget to change it when your production server’s ip address changes. Not an issue if you use just two computers with 127.0.0.1 and “else”.
Running scripts locally
Sometimes you need to run a script locally as an extra level of security. Say you run a cron job and you don’t want public to mess with it by running it off schedule. So you run it as http://localhost/tadada.php and check for SERVER_NAME/ADDR to make sure it was indeed run that way. But then your cannot use it for picking the right configuration set.
getenv(“COMPUTERNAME”) to the rescue
This value is unlikely to change during the lifetime of your application and doesn’t change based on the way you run it. The drawback is that not all servers define this variable so you may have to seek a different computer identificator.
What is your way of dealing with configuration sets?