Jump to content

CloudFlare IP Geolocation - elseif help


essypowers

Recommended Posts

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

Link to comment
Share on other sites

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
 
 
}
?>
Link to comment
Share on other sites

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 by mac_gyver
Link to comment
Share on other sites

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
 
----
Link to comment
Share on other sites

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
 
 
}
?>
Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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 } ?>
Link to comment
Share on other sites

  • 4 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.