Jorge Y. C. Rodriguez
Jorge Y. C. Rodriguez

Reputation: 3449

Get the continent/country or location of user in php?

Is possible to get the continent from where the user it is accessing a site? I do know i can get the language of the browser like this:::

<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
    case "fr":
        //echo "PAGE FR";
        include("index_fr.php");//include check session FR
        break;
    case "it":
        //echo "PAGE IT";
        include("index_it.php");
        break;
    case "en":
        //echo "PAGE EN";
        include("index_en.php");
        break;        
    default:
        //echo "PAGE EN - Setting Default";
        include("index_en.php");//include EN in all other cases of different lang detection
        break;
}
?>

REF

Also i do know there is something php function call -> geoip_continent_code_by_name which if a pass a string with the host name it will return me:

Code Continent name

AF  Africa
AN  Antarctica
AS  Asia
EU  Europe
NA  North america
OC  Oceania
SA  South america

But this only returns me the host name, so is there any way to get the same result but for the user who is accessing the site? or is even possible to do?

Upvotes: 3

Views: 6196

Answers (2)

Jamie
Jamie

Reputation: 808

I recommend using the Maxmind GeoLite2 database starting out. It is has a creative commons license, and you can upgrade to the commercial GeoIP2 database at a later time if you need more complete IP coverage. The country and continent codes are available in this database.

I describe how to compile the extension, and how to use the mmdb databases in PHP here:

Intro to Maxmind GeoLite2 with Kohana PHP

enter image description here

Upvotes: 1

Tim B
Tim B

Reputation: 1877

You can acquire a geo location database based on the user's IP address in $_SERVER["REMOTE_ADDR"] and determine the city / country from there.

MaxMind provides a free city/country database here: http://dev.maxmind.com/geoip/geoip2/geolite2/

Upvotes: 1

Related Questions