essypowers Posted March 30, 2014 Share Posted March 30, 2014 Hi Guys... new to php... just signed up for CloudFlare and trying to use IP Geolocation any help would be appreciated... the goal is for the selected countries to show maintenance message. Otherwise show the content of the page this is what I have but its not working... <?php $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; if ($country_code=="FR") {echo "Site Under Maintenance";}elseif ($country_code=="UK") {echo "Site Under Maintenance";}elseif ($country_code=="US") {echo "Site Under Maintenance";} else { } ?> Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/287390-cloudflare-ip-geolocation-elseif-help/ Share on other sites More sharing options...
QuickOldCar Posted March 30, 2014 Share Posted March 30, 2014 This should work fine <?php $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; $message = "Site Under Maintenance"; if ($country_code == "FR") { echo $message; }elseif ($country_code == "UK") { echo $message; }elseif ($country_code == "US") { echo $message; }else { //place your code this area to show it } ?> Quote Link to comment https://forums.phpfreaks.com/topic/287390-cloudflare-ip-geolocation-elseif-help/#findComment-1474416 Share on other sites More sharing options...
mac_gyver Posted March 30, 2014 Share Posted March 30, 2014 (edited) what debugging have you done? does $_SERVER["HTTP_CF_IPCOUNTRY"] exist and contains a value you expect? and rather than to write out a series of conditional statements to test if a variable is one of several possible values, it's best to make an array of the expected values and use in_array() to test if a variable is one of the values in the array. Edited March 30, 2014 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/287390-cloudflare-ip-geolocation-elseif-help/#findComment-1474417 Share on other sites More sharing options...
essypowers Posted March 30, 2014 Author Share Posted March 30, 2014 Thanks for the reply guys... This should work fine <?php $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; $message = "Site Under Maintenance"; if ($country_code == "FR") { echo $message; }elseif ($country_code == "UK") { echo $message; }elseif ($country_code == "US") { echo $message; }else { //place your code this area to show it } ?> @QuickOldCar This was great... I am seeing few issues When i use one IF statement... the message displays <?php$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];$message = "Site Under Maintenance"; if ($country_code == "US") {echo $message;}else { }?> But when I introduce the ELSEIF s the message no longer appears... what debugging have you done? does $_SERVER["HTTP_CF_IPCOUNTRY"] exist and contains a value you expect? and rather than to write out a series of conditional statements to test if a variable is one of several possible values, it's best to make an array of the expected values and use in_array() to test if a variable is one of the values in the array. @mac_gyver I ran $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; echo "Country code is " . $country_code; and got the expected results, also as mentioned above one IF statement works like a charm ---- Quote Link to comment https://forums.phpfreaks.com/topic/287390-cloudflare-ip-geolocation-elseif-help/#findComment-1474418 Share on other sites More sharing options...
QuickOldCar Posted March 30, 2014 Share Posted March 30, 2014 No idea why that if/else doesn't work, try as mac_gyver suggested using a check in an array <?php $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; $not_allowed = array("FR","UK","US"); if (in_array($country_code, $not_allowed)) { echo "Site Under Maintenance"; } else { //place your code this area to show it } ?> Quote Link to comment https://forums.phpfreaks.com/topic/287390-cloudflare-ip-geolocation-elseif-help/#findComment-1474422 Share on other sites More sharing options...
essypowers Posted March 30, 2014 Author Share Posted March 30, 2014 No idea why that if/else doesn't work, try as mac_gyver suggested using a check in an array <?php $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; $not_allowed = array("FR","UK","US"); if (in_array($country_code, $not_allowed)) { echo "Site Under Maintenance"; } else { //place your code this area to show it } ?> Guys thank you... the arry approach works perfectly @QuickOldCar - Puttig the HTML content in the final else container throws all kinds of syntax errors I am wondering is there is function that we can use to stop the scripy if the first IF statement is called... and if nothing is returned load the page... Thanks One last question... when i put the HTML code Quote Link to comment https://forums.phpfreaks.com/topic/287390-cloudflare-ip-geolocation-elseif-help/#findComment-1474426 Share on other sites More sharing options...
QuickOldCar Posted March 31, 2014 Share Posted March 31, 2014 Ok, you need to know how and when to escape in and out of php to html. http://www.php.net/manual/en/language.basic-syntax.phpmode.php If is within php you need to echo and properly wrap in quotes You can also just break out of php and then back in like so <?php $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; $not_allowed = array("FR","UK","US"); if (in_array($country_code, $not_allowed)) { echo "Site Under Maintenance"; } else { ?> <html> <body> </body> </html> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/287390-cloudflare-ip-geolocation-elseif-help/#findComment-1474473 Share on other sites More sharing options...
xtrimsky Posted August 11, 2014 Share Posted August 11, 2014 Please note: the country code "UK" doesn't work with Cloudflare, it has to be "GB". Quote Link to comment https://forums.phpfreaks.com/topic/287390-cloudflare-ip-geolocation-elseif-help/#findComment-1487397 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.