MissSarah746482
MissSarah746482

Reputation: 13

How do I redirect users from a specific country?

I would like to be able to detect if visitors to a webpage are situated in the USA and if they are I would like to redirect them to a different page.

I've been googling for some script that could do this but haven't found anything. Can someone point me in the right direction?

I'd ideally like to be able to achieve it with Javascript and/or HTML5 but if that's not possible please do let me know about alternatives.

Upvotes: 1

Views: 3117

Answers (2)

Miroslav Blstak
Miroslav Blstak

Reputation: 1

There are some scripts to solve your problem (in PHP): http://www.phptutorial.info/iptocountry/the_script.html

Summary:

  1. $_SERVER['REMOTE_ADDR'] = variable to detect IP address of visitor
  2. download list of IP address ranges on page above (you can choose specific list of countries or download all countries) - each country has range of IP addreses

aletrnative solution is to use webservice http://freegeoip.net/, but there is daily limit for requests.

Upvotes: 0

Tasos
Tasos

Reputation: 7577

A tutorial from here: GeoDirection

<script language="Javascript" src="http://gd.geobytes.com/gd?after=-1&variables=GeobytesLocationCode,GeobytesCode,GeobytesInternet"></script>
<script language="Javascript">
if(typeof(sGeobytesLocationCode)=="undefined"
   ||typeof(sGeobytesCode)=="undefined"
   ||typeof(sGeobytesInternet)=="undefined")
{
   // Something has gone wrong with the variables, so set them to some default value,
   // maybe set a error flag to check for later on.
   var sGeobytesLocationCode="unknown";
   var sGeobytesCode="unknown";
   var sGeobytesInternet="unknown";
}
if(sGeobytesLocationCode=="GRATATHE")
{
   // Visitors from Athens would go here
   window.open("enter Athens URL here");
}else if(sGeobytesCode=="AT")
{
   // Visitors from Attiki would go here
   window.open("enter Attiki URL here");
}else if(sGeobytesInternet=="GR")
{
   // Visitors from Greece would go here
   window.open("enter Greece URL here");
}
</script>

Upvotes: 2

Related Questions