I have created a script for determining the country of a user to give them content specific to the country (e.g. currencies, etc,.).
This script is included in the header of all pages on the site because I want them to be able to change country if they want to (e.g. they might be abroad but still want content from home).
If it finds the posted variable it will change the country and redirect back to the home page. This will cause a redirect loop however, as the script is also included on this and other pages which then looks for the posted variable again but can't find it.
I am not giving custom content for all countries just specific countries I have saved in a database.
My script is as follows
<?php
$ip;
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$content = file_get_contents('https://tools.keycdn.com/geo.json?host='.$ip);
$jsonArray = json_decode($content, true);
$country = "";
foreach($jsonArray['data']['geo'] as $key => $value){
if($key != 'country_name'){
}else{
$country = $value;
}
}
$active_country_id;
$active_country_name = "";
$active_country_code;
$active_country_currency;
$country_active = "";
$country_menu = "";
if(!isset($_GET["country_id"])){
$sql1 = mysqli_query($con, "SELECT * FROM countries WHERE country='$country'");
$countryCount1 = mysqli_num_rows($sql1);
if ($countryCount1 > 0) {
while($row = mysqli_fetch_array($sql1)){
$active_country_id = $row["id"];
$active_country_name = $row["country"];
$active_country_code = $row["code"];
$active_country_currency = $row["currency"];
$country_active .= "$active_country_code";
$country_active .= " - ";
$country_active .= "$active_country_name";
}
} else {
$country_active = "No Countries";
}
}else{
$new_country_id = $_GET["country_id"];
$sql1 = mysqli_query($con, "SELECT * FROM countries WHERE id='$new_country_id'");
$countryCount1 = mysqli_num_rows($sql1);
if ($countryCount1 > 0) {
while($row = mysqli_fetch_array($sql1)){
$active_country_id = $row["id"];
$active_country_name = $row["country"];
$active_country_code = $row["code"];
$active_country_currency = $row["currency"];
$country_active .= "$active_country_code";
$country_active .= " - ";
$country_active .= "$active_country_name";
}
} else {
$country_active = "No Countries";
}
//header("location: index.php");
}
$sql2 = mysqli_query($con, "SELECT * FROM countries WHERE country!='$active_country_name'");
$countryCount2 = mysqli_num_rows($sql2);
if ($countryCount2 > 0) {
while($row = mysqli_fetch_array($sql2)){
$country_id = $row["id"];
$country_name = $row["country"];
$country_code = $row["code"];
$country_currency = $row["currency"];
$country_menu .= "<li><a href='country.php?country_id=$country_id'>";
$country_menu .= "$country_code";
$country_menu .= " - ";
$country_menu .= "$country_name";
$country_menu .= "</a></li>";
}
} else {
$country_menu = "No other Countries";
}
?>
I want to perform the redirect on the commented out line //header("location: index.php");
Any help with this would be greatly appreciated.
You’ve put your redirection in the else
where country_id
exists as a request. You should look for where country_id
is missing, then redirect with country_id
.
It will be easier to read your own code if you refactor it into
if (true) {
// stuff
} else {
// other stuff
}
If you use if (!$something)
followed by an else
, the else becomes a double negative (not not) and it’s confusing to read. Try using if ($_GET...)
first, then an else
.
Or... you could also just not have an else
and design your script around only doing a redirect when required, otherwise letting it carry on.
if (!$_GET['country_id']) {
// redirect with country_id
}
// Carry on with page as normal