开发者

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

开发者 https://www.devze.com 2023-01-27 06:53 出处:网络
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 the

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 th开发者_运维知识库at 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?


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 :)


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.


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";


Any checking usualy go by IP and using some database of ipranges and to what country thay are assigned.

This works in most cases unless the user uses some proxy or likewize.

Most of these cost money or is only avaiable for certain purposses.

Another solution but not as reliable is to use the browsers langugae and possibly if you can query for timezone.

I do not have any specific services to recommend as most of them do not allow commercial use and hence I cannot use them ;)


Here is a solution (actually 3 solutions) that Google recommends:

GeoLocation by Google

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号