Jump to content

Automatically detect country?


markfiend

Recommended Posts

At the moment I have this snippet on my index page: [code]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");
}[/code] 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.
Link to comment
https://forums.phpfreaks.com/topic/10953-automatically-detect-country/
Share on other sites

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']
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.

[!--quoteo(post=379105:date=Jun 1 2006, 11:02 AM:name=gnuffo1)--][div class=\'quotetop\']QUOTE(gnuffo1 @ Jun 1 2006, 11:02 AM) [snapback]379105[/snapback][/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... [code]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);
}[/code] 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\" /]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.