Geographic redirection using hostip.info API
by pixeldublu on Iul.01, 2010, under Tutorials
![]()
A good solution for those who have sites in more languages. If you want visitors to be automatically redirected to the correct page you can use a simple php code and an API from hostip.info. hostip.info offers free IP to location. Unfortunately, cities are hardly detected correctly, but country of origin is detected correctly.
Add this code to your page and change the variables and locations as you want.
// Get user ip
$ip = getenv("REMOTE_ADDR");
// Generate hostip page
$hostip = "http://api.hostip.info/country.php?ip=".$ip;
// Read hostip
$rezultat = file($hostip);
// Rule if
if ( $rezultat[0] == "CH" ) {
header("Location: Chinese.html");
} else {
header("Location: English.html");
}




