Jump to content


Photo

Automatically detect country?


  • Please log in to reply
3 replies to this topic

#1 markfiend

markfiend
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 01 June 2006 - 01:47 PM

At the moment I have this snippet on my index page:
if (isset($_SESSION['country']))
{
    if ($_SESSION['country'] == "UK")
    {
        header("Location: UK_index.php");
    }
    else
    {
        header("Location: restofworld_index.php");
    }
}
else
{
    header("Location: set_country.php");
}
so the browser is pointed to the relevant page if the session 'country' variable is set, and a page to set it if not.

That all works fine and dandy, but I'd rather people weren't diverted to a "please choose your country" page when they first arrive at the site; I think it could put visitors off.

I found the [a href=\"http://www.phpfreaks.com/quickcode/IP-logger/624.php\" target=\"_blank\"]IP Logger[/a] in the code library but I've tried it and it returns my country as "com" even though I'm in the UK [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]

So is there any other way for php to tell me from what country someone is browsing to my site?

Thanks in advance folks.

#2 gnuffo1

gnuffo1
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 01 June 2006 - 04:02 PM

This site [a href=\"http://api.hostip.info/?ip=\" target=\"_blank\"]http://api.hostip.info/?ip=[/a][ip address] produces an xml document giving details about the IP address, including the country. So it would probably be quite easy to write a function that gets the country code from there based on $_SERVER['REMOTE_ADDR']
[a href="http://www.sentinelpc.co.uk" target="_blank"]Sentinel Technology - Custom PCs and Laptops[/a]

#3 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 01 June 2006 - 04:11 PM

There are a couple of Free IP2Country databases out there.

What it is is a list of all IP addresses registered to countries. You would take the REMOTE_ADDR of someoen visiting your website, convert their IP to the numerical representation of their IP and then cross reference that with the table.

I dont have access to my information on the formula for the conversion or the address of one of the free databases.



#4 markfiend

markfiend
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 02 June 2006 - 09:03 AM

[!--quoteo(post=379105:date=Jun 1 2006, 11:02 AM:name=gnuffo1)--][div class=\'quotetop\']QUOTE(gnuffo1 @ Jun 1 2006, 11:02 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
This site [a href=\"http://api.hostip.info/?ip=\" target=\"_blank\"]http://api.hostip.info/?ip=[/a][ip address] produces an xml document giving details about the IP address, including the country. So it would probably be quite easy to write a function that gets the country code from there based on $_SERVER['REMOTE_ADDR']
[/quote]
Great, fantastic, that's what I was looking for. And indeed the function is pretty easy...
function ipToCountry($ip='') 
{
   if ($ip=='') $ip = $_SERVER['REMOTE_ADDR'];
    
    $file = fopen (("http://api.hostip.info/?ip=" . $ip), "r");
    if (!$file) 
    {
         echo "<p>Unable to open remote file.\n";
         exit;
    }
    while (!feof ($file)) 
    {
         $line = fgets ($file, 1024);
         if (eregi ("<countryName>(.*)</countryName>", $line, $out)) 
         {
             $country = $out[1];
             return $country;
         }
    }    
    fclose($file);
}
The function ipToCountry() returns "UNITED KINGDOM" for me here, and ipToCountry('203.26.206.130') returns "AUSTRALIA" (just a random example).

Thanks very much. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users