简体   繁体   中英

PHP header location redirect loop

I found this php geo-redirect script here

Here is the code:

<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
// create a variable for the country code
$var_country_code = $geoplugin->countryCode;
// redirect based on country code:
if ($var_country_code == "US") {
header('Location: http://domain.com');
}
else if ($var_country_code == "NL") {
header('Location: http://domain.com/nl');
}
else if ($var_country_code == "FR") {
header('Location: http://domain.com/fr');
}
else {
header('Location: http://domain.com/int');
}
?>

I placed this code at the very beginning of my index.php.

The problem which I am getting and trying to solve is that when my IP is US based redirect script should redirect to itself and stay on home page. Instead I am getting some redirect loop and page doesn't load properly.

If location is US, then no need to redirect:

<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
// create a variable for the country code
$var_country_code = $geoplugin->countryCode;
// redirect based on country code:
if ($var_country_code == "US") {
   //header('Location: http://domain.com');
}
else if ($var_country_code == "NL") {
header('Location: http://domain.com/nl');
}
else if ($var_country_code == "FR") {
header('Location: http://domain.com/fr');
}
else {
header('Location: http://domain.com/int');
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM