How to get user language by ip address using php?

I need to get the language of a user who visits my site at their IP address using PHP. how can I do this, is there any API for this, please inform

+4
source share
5 answers

You can use any geoIP module. This allows you to determine the country by IP. But this is not entirely correct. For example, I am now in Thailand (and I have Thai IP), but my language is Russian :)

This is not very good when google shows me a page in Thai.

You can use the Headers from the browser to determine the language. Preferred languages ​​are listed in these headers.

+5
source

Use the GeoIP module as previously suggested, and then paste this code somewhere in your project:

if($country = geoip_country_code_by_name($host)) { switch($country) { case "DJ": case "ER": case "ET": $lang = "aa"; break; case "AE": case "BH": case "DZ": case "EG": case "IQ": case "JO": case "KW": case "LB": case "LY": case "MA": case "OM": case "QA": case "SA": case "SD": case "SY": case "TN": case "YE": $lang = "ar"; break; case "AZ": $lang = "az"; break; case "BY": $lang = "be"; break; case "BG": $lang = "bg"; break; case "BD": $lang = "bn"; break; case "BA": $lang = "bs"; break; case "CZ": $lang = "cs"; break; case "DK": $lang = "da"; break; case "AT": case "CH": case "DE": case "LU": $lang = "de"; break; case "MV": $lang = "dv"; break; case "BT": $lang = "dz"; break; case "GR": $lang = "el"; break; case "AG": case "AI": case "AQ": case "AS": case "AU": case "BB": case "BW": case "CA": case "GB": case "IE": case "KE": case "NG": case "NZ": case "PH": case "SG": case "US": case "ZA": case "ZM": case "ZW": $lang = "en"; break; case "AD": case "AR": case "BO": case "CL": case "CO": case "CR": case "CU": case "DO": case "EC": case "ES": case "GT": case "HN": case "MX": case "NI": case "PA": case "PE": case "PR": case "PY": case "SV": case "UY": case "VE": $lang = "es"; break; case "EE": $lang = "et"; break; case "IR": $lang = "fa"; break; case "FI": $lang = "fi"; break; case "FO": $lang = "fo"; break; case "BE": case "FR": case "SN": $lang = "fr"; break; case "IL": $lang = "he"; break; case "IN": $lang = "hi"; break; case "HR": $lang = "hr"; break; case "HT": $lang = "ht"; break; case "HU": $lang = "hu"; break; case "AM": $lang = "hy"; break; case "ID": $lang = "id"; break; case "IS": $lang = "is"; break; case "IT": $lang = "it"; break; case "JP": $lang = "ja"; break; case "GE": $lang = "ka"; break; case "KZ": $lang = "kk"; break; case "GL": $lang = "kl"; break; case "KH": $lang = "km"; break; case "KR": $lang = "ko"; break; case "KG": $lang = "ky"; break; case "UG": $lang = "lg"; break; case "LA": $lang = "lo"; break; case "LT": $lang = "lt"; break; case "LV": $lang = "lv"; break; case "MG": $lang = "mg"; break; case "MK": $lang = "mk"; break; case "MN": $lang = "mn"; break; case "MY": $lang = "ms"; break; case "MT": $lang = "mt"; break; case "MM": $lang = "my"; break; case "NP": $lang = "ne"; break; case "AW": case "NL": $lang = "nl"; break; case "NO": $lang = "no"; break; case "PL": $lang = "pl"; break; case "AF": $lang = "ps"; break; case "AO": case "BR": case "PT": $lang = "pt"; break; case "RO": $lang = "ro"; break; case "RU": case "UA": $lang = "ru"; break; case "RW": $lang = "rw"; break; case "AX": $lang = "se"; break; case "SK": $lang = "sk"; break; case "SI": $lang = "sl"; break; case "SO": $lang = "so"; break; case "AL": $lang = "sq"; break; case "ME": case "RS": $lang = "sr"; break; case "SE": $lang = "sv"; break; case "TZ": $lang = "sw"; break; case "LK": $lang = "ta"; break; case "TJ": $lang = "tg"; break; case "TH": $lang = "th"; break; case "TM": $lang = "tk"; break; case "CY": case "TR": $lang = "tr"; break; case "PK": $lang = "ur"; break; case "UZ": $lang = "uz"; break; case "VN": $lang = "vi"; break; case "CN": case "HK": case "TW": $lang = "zh"; break; default:break; } } 
+5
source

Although you can search by IP address to get an idea of ​​the overall physical location, the physical location may not have much to do with the language of the person whose IP address you are viewing.

For example, if the IP location may be the New York area of ​​the United States, a person can speak Mandarin as their primary language because they visit the area.

Here is a service you can use to find the IP address http://ipinfodb.com/ip_location_api.php

Here is a discussion of stackoverflow Getting a user's location by IP address in C # , which may also be useful.

However, you will need to provide a mechanism for changing the language. And use cookies or something similar to remember the choice of language.

The most common use of location information is to provide targeted advertising, which is likely to be beneficial to the person in that place.

EDIT: Using Mobile Devices

As mobile devices and smartphones accessing the Internet through high-speed 3G and 4G cellular networks are becoming more common, the question arises whether IP geolocation works for these types of devices.

The short answer is not very good. See This article, Where is this phone ?: Geolocation of IP addresses in 3G networks from Microsoft Research.

In addition, here is a question about the stacking surface in the location of a mobile phone from an HTTP request .

Here's an article, Geolocation of IP Addresses in Cellular Data Networks , which also contains some information about the problem.

+1
source
  function ipToCountry ($ ip)
     {
     $ info = file_get_contents ("http://who.is/whois-ip/ip-address/$ip");
     list ($ a, $ b) = explode ('COUNTRY:', strtoupper ($ info));
     return substr (trim ($ b), 0,2);
     }

     function geLangCode ()
     {
     $ countryCode = library :: ipToCountry ($ _ SERVER ['REMOTE_ADDR']);
     return $ yourQuery
     }
0
source

You can try my API, https://ipinfo.io . It returns JSON by default, with a bunch of different fields:

 $ curl ipinfo.io { "ip": "24.6.61.239", "hostname": "c-24-6-61-239.hsd1.ca.comcast.net", "city": "Mountain View", "region": "California", "country": "US", "loc": "37.3845,-122.0881", "org": "AS7922 Comcast Cable Communications, LLC", "postal": "94040" } 

See https://ipinfo.io/developers for more details.

If you do this to determine the language, you better look at the Accept-Language heading, although it is set based on your preferences, rather than wondering what language they can speak based on their current location.

0
source

All Articles