There are many method of detecting which manner of operation your various sites have, the simplest of which is a variable that states the mode in a common file. This one file can become a hazard because you need to remember not to sync that file to the production server.
I was looking for a manner by which to determine which server the code was executing on with a high-degree of reliability and this seems to work. I created the function called 'serverIPAddress()' which returns the obvious, but it's the how that is interesting. I must say, I am unsure whether this will work 100% for all of you as-is, as there many be slight differences in the output of the Linux command 'ifconfig' that need to be accounted for. In my implementation there are but two adapters, local (lo) and Ethernet (eth0), eth0 is listed first and I'm not sure this can change but I'll accept that it could. So, that said... the output of ifconfig looks like this:
eth0 Link encap:Ethernet HWaddr 00:0C:29:41:18:C4The code to extract the 'inet addr' from this response looks like this:
inet addr:192.168.1.3 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe41:18c4/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:26995 errors:0 dropped:0 overruns:0 frame:0
TX packets:20748 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3324252 (3.1 MiB) TX bytes:6947293 (6.6 MiB)
Interrupt:177 Base address:0x1400
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
function serverIpAddress(){The path for ifconfig is explicit due my use of this in a host of scheduled tasks (cron jobs) on the server. I'll be apply in this to my web site as well as the method for detection is based on 'SERVER_NAME' and a little less reliable/safe in my books.
exec("/sbin/ifconfig",$ifconfig);
$target=explode(" ",$ifconfig[1]);
$bustoutip=explode(":",$target[5]);
$serverIP=$bustoutip[1];
return $serverIP;
}
Once I have the IP Address I can move on to making server specific code that understands what to do dependant upon the server that it is executing on. The following code takes the resulting IP Address and sets variables, etc. to ensure that code transitioned from development to stage/production can understand and behave reliably. There's nothing worse during service recovery when there are code differences, especially if the code differences are not well documented.
switch(serverIPAddress()){
case '192.168.1.3': // PRODUCTION$enable_say = 1;case '192.168.1.9': // STAGE
define( "DEBUG", false);
$SMTPSmartHost = "192.168.1.7"; // Kerio Server
define("K_ServerMode","P");
break;$enable_say = 1;case '192.168.1.45': // DEV
define( "DEBUG", false); //DEV
$SMTPSmartHost = "192.168.1.2"; // Cameron's DEV VM
define("K_ServerMode","S");
break;
default:$enable_say = 1;}
define( "DEBUG", true); //DEV
$SMTPSmartHost = "192.168.1.2"; // Cameron's DEV VM
define("K_ServerMode","D");
break;