Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best solution to check a users country?

Tags:

php

Services that give API's

I read/heard that there are a lots of services that gives API's to do this, however I don't really want to rely on anybody so please if there is a way to do it without them let me know.

Server Side

And also I read/heard that this is done in server side coding what makes me confused, I always thought that this is done by Javascript.

IP list

And I also read/heard that those services that gives API's they have a very huge list of IP's from ISP's and they can determine the country by this list, is this true? And if I want to make it alone without API's, should I have a list too?

like image 942
Adam Halasz Avatar asked Nov 26 '10 16:11

Adam Halasz


3 Answers

When I did my lookup, I used this script : http://www.phptutorial.info/iptocountry/the_script.html

The db is a list of php files and you can subscribe to a list which will notify you of database updates. Pretty useful if you don't have a database :)

like image 180
Victor Parmar Avatar answered Nov 08 '22 21:11

Victor Parmar


MaxMind offers the GeoLite Country IP database, which is also used or supported by some web log analyzers like Webalizer and AWStats.

It is available for free in CSV format which you can import into your application's database. They also provide a comprehensive collection of guides and examples.

like image 45
Fabian Iwand Avatar answered Nov 08 '22 20:11

Fabian Iwand


An easy way to get the users country is to read the HTTP_ACCEPT_LANGUAGE header.

Here an example (PHP):

$langcodes=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
$host=gethostbyaddr( $_SERVER['REMOTE_ADDR'] );

if(preg_match("/(bg|bg-BG|cs|cs-CZ|da|da-DK|de|de-AT|de-CH|de-DE|de-LI|de-LU|el|el-GR|en-GB|en-IE|es-ES|et|et-EE|fi|fi-FI|fr-BE|fr-CH|fr-FR|fr-LU|fr-MC|ga|hu|hu-HU|it|it-CH|it-IT|lt|lt-LT|lv-LV|lv|mt|mt-MT|nl|nl-BE|nl-NL|pl|pl-PL|pt-PT|ro|ro-RO|sk|sl|sk-SK|sv|sv-FI|sv-SE|fr)([,;].*)?$/i",$langcodes) 
   || preg_match("/(\.ad|\.al|\.at|\.ax|\.ba|\.be|\.bg|\.by|\.ch|\.cy|\.cz|\.de|\.dk|\.ee|\.es|\.eu|\.fi|\.fo|\.fr|\.gb|\.gg|\.gi|\.gr|\.hr|\.hu|\.ie|\.im|\.is|\.it|\.je|\.li|\.lt|\.lu|\.lv|\.mc|\.md|\.me|\.mk|\.mt|\.nl|\.no|\.pl|\.pt|\.ro|\.rs|\.se|\.si|\.sk|\.sm|\.ua|\.uk|\.va)$/i", $host))
{
    echo "European User";
}
else
    echo "non-European User";
like image 37
seizu Avatar answered Nov 08 '22 19:11

seizu