s_ainley87 Posted June 24, 2008 Share Posted June 24, 2008 Hi, I was wondering if this was the best way to tackle a problem, I currently developing a site that will sit on 3 different servers, a localhost, a staging server and a live server, my problem is that I am using zend_mail to send form data to a mail recipient however the 3 different servers use different methods of sending that mail, what be the best way of detecting which sever it is and changing the method it uses and how would I construct it? Link to comment https://forums.phpfreaks.com/topic/111637-php-switch-statement/ Share on other sites More sharing options...
ScotDiddle Posted June 24, 2008 Share Posted June 24, 2008 s_ainley87, This is one possible solution... Scot L. Diddle, Richmond VA [pre] <?php $mailEnvelope = array(); $serverIP = $_SERVER['SERVER_ADDR']; $serverPort = $_SERVER['SERVER_PORT']; // $serverIP = '777.888.999.555'; // Hand Code to test new values // $serverName = $_SERVER['SERVER_NAME']; // Use if known.. $sendMailParms = setMailEnvelope($serverIP, $serverPort); var_dump($sendMailParms); exit; function setMailEnvelope($serverIP, $serverPort) { switch ($serverIP) { case '777.888.999.555' : $mailServer = 'HOST1'; // Or: $mailServer = $serverName; $mailMethod = 'SENDMAIL'; $serverStatus = determineServerStatus($serverPort); break; case '222.333.444.555' : $mailServer = 'HOST2'; // Or: $mailServer = $serverName; $mailMethod = 'SMTP'; $serverStatus = determineServerStatus($serverPort); break; case '127.0.0.1' : $mailServer = 'LOCALHOST'; // Or: $mailServer = $serverName; $mailMethod = 'SNAIL_MAIL'; $serverStatus = determineServerStatus($serverPort); break; default : echo "Could not Determine \$serverIP... Exiting <br />"; exit; break; } // END switch ($serverIP) { $mailEnvelope['hostName'] = $mailServer; $mailEnvelope['method'] = $mailMethod; $mailEnvelope['serverStatus'] = $serverStatus; return $mailEnvelope; } // END function setMailEnvelope($serverIP, $serverPort) { function determineServerStatus($serverPort) { switch($serverPort) { case '80' : $serverStatus = 'PROD'; break; case '8080' : $serverStatus = 'TEST'; break; case '9000' : $serverStatus = 'QA'; break; default : echo "Could not determine \$serverPort... Exiting. <br />"; exit; break; } Return $serverStatus; } // END function determineServerStatus($serverPort) { ?> Link to comment https://forums.phpfreaks.com/topic/111637-php-switch-statement/#findComment-573125 Share on other sites More sharing options...
s_ainley87 Posted June 24, 2008 Author Share Posted June 24, 2008 Sorry could you explain your code a little? Link to comment https://forums.phpfreaks.com/topic/111637-php-switch-statement/#findComment-573225 Share on other sites More sharing options...
ScotDiddle Posted June 24, 2008 Share Posted June 24, 2008 s_ainley87, Sure, First I built an array to hold the results we are looking for. $mailEnvelope = array(); This will end up holding : $mailEnvelope['hostName'] = $mailServer; $mailEnvelope['method'] = $mailMethod; $mailEnvelope['serverStatus'] = $serverStatus; Which is what you are looking for. This is determined by asking PHP to return some built-in values: $serverIP = $_SERVER['SERVER_ADDR']; $serverPort = $_SERVER['SERVER_PORT']; The first value received from the built-in $_SERVER array is the IP of the server where the script is running. The second value received is the port number on the server where the script is running. The statement : $sendMailParms = setMailEnvelope($serverIP, $serverPort); calls the setMailEnvelope function which will have the IP addresses hand-coded for your various servers... The ones shown in the example are dummys, and do no exist. While developing this, my own IP address was passed, (172.16.238.147) to the first function, and my port: 8080 was passed, because I was running as DEV. I had to hand-code the first case statement to: case '172.16.238.147' : // MY IP If you are only dealing with three servers, change the case statment paramaters to match the IP addresses. If you are dealing with more that three servers, you will need to add an new: case 'IP Address to Check' : // set your values here... break; as well as new: case 'PORT NUMBER to Check' : // set your values here... break; in the second function, if you have additional port numbers. Also, you will have to change the port numbers to match your environment. When a match is found (i.e., the script knows which server is is running on) it sets the parameters to be returned. Each case statment from the original function, when hit, also, calls: $serverStatus = determineServerStatus($serverPort); to determine the server status of the server the script is running on. ( It asks: Am I running on a production, development, or QA system ?) This will only be important or useful, if you have 2 ( or more ) apache servers running on one box. I have PROD and DEV both running under the same apache, but using different httpd.conf files, PROD uses port 80, while DEV uses port 8080. If you need your mailing script to behave differently depending on which port the php script is running on, the call to the second function provides this. The function returns the server status... Again this is up to you... You may not have a port 9000 in use for QA ( Quality Assurance ), and you can remove the case statement and it's assignment, and it's closing 'break' statment. After the original function has ended the information you will need later to determine which "SENDMAIL" environment you need to code for will be found in the "now filled in array" called: $mailEnvelope, which is returned to the original function, and fill in the value for : $sendMailParms The var_dump statement is purely for debugging, so you can see if you get the expected results back. Once you are satisfied, you can delete or comment-out the var_dump statement. You can now do something like: $host = $sendMailParms['hostname'] switch ($host) { case 'HOST1' : // code goes here for what ever HOST1 needs to do to send your mail.. break; case 'HOST2' : // code goes here for what ever HOST2 needs to do send to your mail.. break; // ... etc } And there you have it. Hope my explanation was satisfactory... If not, let me know, and we will get this to work for you. Scot L. Diddle Link to comment https://forums.phpfreaks.com/topic/111637-php-switch-statement/#findComment-573447 Share on other sites More sharing options...
s_ainley87 Posted June 25, 2008 Author Share Posted June 25, 2008 Thanks I will give this try and get back to you Link to comment https://forums.phpfreaks.com/topic/111637-php-switch-statement/#findComment-573964 Share on other sites More sharing options...
s_ainley87 Posted June 25, 2008 Author Share Posted June 25, 2008 Hi sorry I am probably being really dumb, but could explain how i would do this code if my servers were called... localhost, staging and live? Link to comment https://forums.phpfreaks.com/topic/111637-php-switch-statement/#findComment-574418 Share on other sites More sharing options...
ScotDiddle Posted June 26, 2008 Share Posted June 26, 2008 s_ainley87, Where my example has The IP's for HOST1, HOST2, and LOCALHOST hard-coded, for: case '777.888.999.555' : $mailServer = 'HOST1'; // Or: $mailServer = $serverName; $mailMethod = 'SENDMAIL'; $serverStatus = determineServerStatus($serverPort); break; change '777.888.999.555' to the IP address of your 'live' server, then change 'HOST1' to 'live', and change 'SENDMAIL' to whichever mail method is used on that server. Make similiar changes to the other two case statements. If you don't know, or are un-sure what the IP address is, after: $mailEnvelope = array(); $serverIP = $_SERVER['SERVER_ADDR']; $serverPort = $_SERVER['SERVER_PORT']; enter: echo "\$serverIP = ' . $serverIP . "<br /><br /> \n"; to find the IP, and: echo "\$serverPort = ' $serverPort . "<br /><br /> \n"; to find the port number. Follow this with: exit(); When you run the script on each sever, you will then have the values you need to complete the switch statements. Scot Link to comment https://forums.phpfreaks.com/topic/111637-php-switch-statement/#findComment-574934 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.