How to get user language by ip address using php?

非 Y 不嫁゛ 提交于 2019-12-10 15:05:33

问题


I need to get language of a user who have visiting my website by their IP address using PHP. how can I do it, there is any API to do this, please advise


回答1:


Use the GeoIP module as has previously been suggested and then insert 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;
    }
}



回答2:


You can use any geoIP module. It allow you detect country by IP. But this is not very correct way. For example I now in Thailand (and I have thai IP), but my language is russian :)

This is not very good when google show me page in thai language.

For detect language you can use Headers from browser. Preferred languages are listed in this Headers.




回答3:


While you can do a lookup on an IP address to get an idea of the general physical location, the physical location may not have much to do with the language of the person whose IP address you are looking up.

For instance while the IP location may be New York area of the United States the person may speak Mandarin as their primary language because they are visiting the area.

Here is a service that you could use for IP address lookup http://ipinfodb.com/ip_location_api.php

Here is a stackoverflow discussion Get user location by IP address in C# that might be helpful as well.

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

The most common use of location information is to provide targeted advertising which is more likely to be of use to the person at that location.

EDIT: Use of mobile devices

Since mobile devices and smart phones accessing the internet over high-speed 3G and 4G cellular networks are becoming increasing common, a question is whether IP address geolocation works for those types of devices.

The short answer is, not very well. See this article, Where's that Phone?: Geolocating IP Addresses on 3G Networks from Microsoft Research.

Also, here is a stackoverflow question on mobile phone location from a HTTP request.

Here is an article, Geolocating IP addressesin Cellular Data Networks that provides some information on the problem as well.




回答4:


    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
    }



回答5:


You could 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're doing this to detect the language you're better off looking at the Accept-Language header though, which is set based on the user's preference rather than guessing what language they might speak based on their current location.



来源:https://stackoverflow.com/questions/13585100/how-to-get-user-language-by-ip-address-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!