[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Script to geolocate user and redirect them to the local website

Posted on 2010-09-10
4
Medium Priority
?
848 Views
Last Modified: 2012-05-10
Hello,

We are looking for a script that we can drop into our site that will geolocate a user downloading a web page a redirect them to a localised version of the website. For example, if a person in UK access the site then they should be redirected to the .co.uk version of the site.

Thanks
0
Comment
Question by:PlumInternet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 668 total points
ID: 33651138
You may use : navigator.userLanguage

Some links :

http://www.cryer.co.uk/resources/javascript/script9.htm
http://xrdp.sourceforge.net/documents/keymap/rfc1766.html
http://www.cryer.co.uk/resources/javascript/script9.htm
http://www.w3.org/TR/html401/references.html#ref-ISO3166
http://fr.wikipedia.org/wiki/ISO_3166-1
http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
http://www.ietf.org/rfc/rfc1766.txt

example of using :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IPHONE REDIRECT PAGE</title>

<script language="javascript">
  switch(navigator.userLanguage.toLowerCase()) {
      case "en-us": window.location = 'http://www.domain.us';break;
      case "en-gb": window.location = 'http://www.domain.co.uk';break;
      case "fr-fr": window.location = 'http://www.domain.fr';break;
      case "de-ch": window.location = 'http://www.domain.ch';break;
      default:      window.location = 'http://www.domain.com';break;
  }
</script>

</head>

<body>

<p>see this - redirect did not work</p>

</body>

</html>

Open in new window

0
 
LVL 10

Assisted Solution

by:bugada
bugada earned 668 total points
ID: 33652703
there is another solution based on IP detection: some sites offer a donwloadble database with association between IP and location.

A free site is http://ip-to-country.webhosting.info/
 
you can download it regularly  using following code to insert it in your database (maybe put this php in crontab).

Then in your home page retrieve the ip of the visitor and ask to db the country associated, do proper redirection.
<?php

require_once('pclzip.lib.php');
ini_set('max_execution_time', 0);

echo "updating ip2location database...";

copy('http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip', 'downloads/ip-to-country.csv.zip');

$archive = new PclZip('./downloads/ip-to-country.csv.zip');
$list = $archive->extract(PCLZIP_OPT_PATH, '.');

$db = mysql_connect("localhost", "root", "password");
mysql_select_db("free2design", $db);

$query = "DELETE FROM free_ip_locations";
$result = mysql_query($query, $db);

$values = '';
$lines = file('ip-to-country.csv');
foreach ($lines as $line) {

$data = explode(',', $line);

$ip_from = trim($data[0], "\"");
$ip_to = trim($data[1], "\"");
$code = trim($data[2], "\"");
$name = trim(trim($data[4]), "\"");

$query = "INSERT INTO free_ip_locations (ip_from, ip_to, code, name) VALUES ($ip_from,$ip_to,'$code','$name')";
$result = mysql_query($query, $db);
}
mysql_close($db);

echo "done";
exit();

?>

Open in new window

0
 
LVL 6

Assisted Solution

by:birwin
birwin earned 664 total points
ID: 33657528
A simple method, using the Google API, can be found at:
http://designshack.co.uk/articles/javascript/detecting-location-using-google-ajax-api#more-1546
I used a more sophisticated method  for a client, of downloading an open source database, and searching it. The results were good, but not consistantly good.
Unless you are willing to pay a monthly fee for the most advanced, commercial databases, and update your database weekly, do not trust the results, and make sure that you have a "Plan B". I had written some code, expecting to always get a location answer, and had to rewrite it, after getting results that I couldn't duplicate. The issue was that some hosts don't supply usable IP address data, so what works on your machine, with your host, won't work on all.
So depending on your need for accuracy you can try:
A free database is available at http://www.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip
A PHP class to read it is available at:
http://phpweby.com/software/ip2country

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33659425
You're welcome! Thanks for the points!
0

Featured Post

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question