Ip2Country没有连接到数据库(PHP)

时间:2011-06-20 19:53:24

标签: php

我有一个mysql数据库来查找IP地址的国家/地区。是否可以在不连接/查询数据库的情况下获取国家/地区?

10 个答案:

答案 0 :(得分:3)

以下是我在我的网站www.vladonai.com上使用的解决方案,它是100%免费(使用每日更新的www.zaigadgets.com/geoip来源),非常快(可能比MySQL快得多),不需要数据库。数据库存储在二进制文件中,因此您需要的是任何版本的PHP。

将IP转换为国家/地区名称的功能如下所示:

function GetCountryFromIP($ip = "")
{
    if ($ip == "")
        $ip = $_SERVER['REMOTE_ADDR'];

    //the db obtained from ZaiGadgets.com and converted using convert_ip2country_db_from_csv_to_binary_file_format function
    $db_file_path = ip2Country_binary_db_filename;
    if (!file_exists($db_file_path))
        return "";
    $db_file_handle = fopen($db_file_path, "rb");
    if (!$db_file_handle)
        return "";

    $one_record_size = 10; //bytes
    $low = 0;
    $high = (filesize($db_file_path) / $one_record_size) - 1;
    $mid = 0;
    $ipFrom = 0;
    $ipTo = 0;

    $ipArrParts = explode('.', $ip);
    $ipLong = ($ipArrParts[0] * 0x1000000) + ($ipArrParts[1] * 0x10000) + ($ipArrParts[2] * 0x100) + ($ipArrParts[3]);

    $country = ""; //result

    while($low <= $high)
    {
        $mid = (int)(($low + $high) / 2);
        $file_pos = $mid * $one_record_size;
        fseek($db_file_handle, $file_pos, SEEK_SET);

        $ipFrom = ipdbv2_read32($db_file_handle);
        $ipTo = ipdbv2_read32($db_file_handle);

        if ($ipFrom < 0) 
            $ipFrom += pow(2, 32);
        if ($ipTo < 0) 
            $ipTo += pow(2, 32);

        if (($ipLong >= $ipFrom) && ($ipLong  < $ipTo))
        {
            //found IP range :)
            //echo "Found!!!!!<br>";
            $country = fread($db_file_handle, 2);
            fclose($db_file_handle);

            $country = GetCountryNameFromCountryCode($country);

            return $country;
        } else
        {
            if($ipLong <$ipFrom)
            {
                $high = $mid - 1;
            } else
            {
                $low = $mid + 1;
            }
        }
    }

    fclose($db_file_handle);

    return "";
}

function ipdbv2_read32($db_file_handle)
{
    $data = fread($db_file_handle, 4);

    $output = unpack('V', $data);
    $val_32_bit = $output[1];

    if ($val_32_bit < 0)
        $val_32_bit += 4294967296; //correct signed/unsigned issue on 32-bit platforms

    return $val_32_bit;
}

在使用上述功能之前,您需要使用以下功能编译数据库文件:

define( "ip2Country_binary_db_filename", "ip2country_zaigadgets.bin" );

function convert_ip2country_db_from_csv_to_binary_file_format($input_file_path = "")
{
    //convert dataf from ZaiGadgets.com to binary db format
    if ($input_file_path == "")
        $input_file_path = "db.csv";
    $output_file_path = ip2Country_binary_db_filename;

    $input_file_content = file_get_contents($input_file_path); //read_text_file_content($input_file_path);

    if ($input_file_content == "")
    {
        echo "Error: empty input db - " . $input_file_path . "<br>";
        return; //nothing to do
    }

    unlink($output_file_path); //delete file content
    $out_fileHandle = fopen($output_file_path, "cb");
    if (!$out_fileHandle)
    {
        echo "ERROR: Cannot Create file! " . $output_file_path . "<br>";
        return;
    } 

    $records_count = 0;
    $input_file_lines = explode("\n", $input_file_content); //split it to lines
    echo "Text lines count: " . count($input_file_lines) . "<br>";

    //for ($input_line_n = 0; $input_line_n < 10; $input_line_n ++)
    for ($input_line_n = 0; $input_line_n < count($input_file_lines); $input_line_n ++)
    {
        $sub_line = $input_file_lines[$input_line_n];
        if (strlen($sub_line) > 0 && is_numeric($sub_line[0]))
        {
            $sub_values = explode(",", $input_file_lines[$input_line_n]);
            if (count($sub_values) == 4)
            {
                $start_ip_addr = $sub_values[0];
                $end_ip_addr = $sub_values[1];
                $country_code = $sub_values[2];
                if (strlen($country_code) == 2)
                {
                    fwrite($out_fileHandle, pack("V", (float)$start_ip_addr), 4);
                    fwrite($out_fileHandle, pack("V", (float)$end_ip_addr), 4);
                    fwrite($out_fileHandle, $country_code, strlen($country_code));
                    $records_count ++;
                } else
                {
                    echo "Invalid line " . $input_line_n . " [wrong country len]: " . $sub_line . "<br>";
                }
            } else
            {
                echo "Invalid line " . $input_line_n . ": " . $sub_line . "<br>";
            }
        } else
        if ($sub_line != "" && $sub_line[0] != '#' && $sub_line != "startip,endip,countrycode,countryname")
        {
            echo "Invalid line " . $input_line_n . ": " . $sub_line . "<br>";
        }
    }
    fclose($out_fileHandle);

    echo "Data size verification result: " . ($records_count * 10 == filesize($output_file_path) ? "OK" : "Error") . "<br>";
    echo "Exported records (blocks) count: " . $records_count . "<br>";
    echo "Exporting complete.<br>";
}

现在,如果您喜欢,可以使用下面的功能将2个字母的国家/地区代码转换为可读的国家/地区名称:

function GetCountryNameFromCountryCode($country_code, $b_very_strict = false)
{
    $countries = array (
        "ac" => "ASCENSION ISLAND",
        "ad" => "ANDORRA",
        "ae" => "UNITED ARAB EMIRATES",
        "af" => "AFGHANISTAN",
        "ag" => "ANTIGUA AND BARBUDA",
        "ai" => "ANGUILLA",
        "al" => "ALBANIA",
        "am" => "ARMENIA",
        "an" => "NETHERLANDS ANTILLES",
        "ao" => "ANGOLA",
        "aq" => "ANTARCTICA",
        "ar" => "ARGENTINA",
        "as" => "AMERICAN SAMOA",
        "at" => "AUSTRIA",
        "au" => "AUSTRALIA",
        "aw" => "ARUBA",
        "ax" => "ALAND",
        "az" => "AZERBAIJAN",
        "ba" => "BOSNIA AND HERZEGOVINA",
        "bb" => "BARBADOS",
        "bd" => "BANGLADESH",
        "be" => "BELGIUM",
        "bf" => "BURKINA FASO",
        "bg" => "BULGARIA",
        "bh" => "BAHRAIN",
        "bi" => "BURUNDI",
        "bj" => "BENIN",
        "bl" => "SAINT BARTHELEMY",
        "bm" => "BERMUDA",
        "bn" => "BRUNEI DARUSSALAM",
        "bo" => "BOLIVIA",
        "bu" => "BONAIRE, SAINT EUSTATIUS AND SABA",
        "br" => "BRAZIL",
        "bs" => "BAHAMAS",
        "bt" => "BHUTAN",
        "bv" => "BOUVET ISLAND",
        "bw" => "BOTSWANA",
        "by" => "BELARUS",
        "bz" => "BELIZE",
        "ca" => "CANADA",
        "cc" => "COCOS (KEELING) ISLANDS",
        "cd" => "CONGO",
        "cf" => "CENTRAL AFRICAN REPUBLIC",
        "cg" => "REPUBLIC OF THE CONGO",
        "ch" => "SWITZERLAND",
        "ci" => "COTE D?VOIRE",
        "ck" => "COOK ISLANDS",
        "cl" => "CHILE",
        "cm" => "CAMEROON",
        "cn" => "CHINA",
        "co" => "COLOMBIA",
        "cr" => "COSTA RICA",
        "cu" => "CUBA",
        "cv" => "CAPE VERDE",
        "cw" => "CURACAO",
        "cx" => "CHRISTMAS ISLAND",
        "cy" => "CYPRUS",
        "cz" => "CZECH REPUBLIC",
        "de" => "GERMANY",
        "dj" => "DJIBOUTI",
        "dk" => "DENMARK",
        "dm" => "DOMINICA",
        "do" => "DOMINICAN REPUBLIC",
        "dz" => "ALGERIA",
        "ec" => "ECUADOR",
        "ee" => "ESTONIA",
        "eg" => "EGYPT",
        "eh" => "WESTERN SAHARA",
        "er" => "ERITREA",
        "eu" => "EUROPEAN UNION",
        "es" => "SPAIN",
        "et" => "ETHIOPIA",
        "fi" => "FINLAND",
        "fj" => "FIJI",
        "fk" => "FALKLAND ISLANDS",
        "fm" => "MICRONESIA",
        "fo" => "FAROE ISLANDS",
        "fr" => "FRANCE",
        "ga" => "GABON",
        "gb" => "UNITED KINGDOM",
        "gd" => "GRENADA",
        "ge" => "GEORGIA",
        "gf" => "FRENCH GUIANA",
        "gg" => "GUERNSEY",
        "gh" => "GHANA",
        "gi" => "GIBRALTAR",
        "gl" => "GREENLAND",
        "gm" => "THE GAMBIA",
        "gn" => "GUINEA",
        "gp" => "GUADELOUPE",
        "gq" => "EQUATORIAL GUINEA",
        "gr" => "GREECE",
        "gs" => "SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS",
        "gt" => "GUATEMALA",
        "gu" => "GUAM",
        "gw" => "GUINEA-BISSAU",
        "gy" => "GUYANA",
        "hk" => "HONG KONG",
        "hn" => "HONDURAS",
        "hr" => "CROATIA",
        "ht" => "HAITI",
        "hu" => "HUNGARY",
        "id" => "INDONESIA",
        "ie" => "IRELAND",
        "il" => "ISRAEL",
        "im" => "ISLE OF MAN",
        "in" => "INDIA",
        "io" => "BRITISH INDIAN OCEAN TERRITORY",
        "iq" => "IRAQ",
        "ir" => "IRAN",
        "is" => "ICELAND",
        "it" => "ITALY",
        "je" => "JERSEY",
        "jm" => "JAMAICA",
        "jo" => "JORDAN",
        "jp" => "JAPAN",
        "ke" => "KENYA",
        "kg" => "KYRGYZSTAN",
        "kh" => "CAMBODIA",
        "ki" => "KIRIBATI",
        "km" => "COMOROS",
        "kn" => "SAINT KITTS AND NEVIS",
        "kp" => "KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF",
        "kr" => "SOUTH KOREA",
        "kw" => "KUWAIT",
        "ky" => "CAYMAN ISLANDS",
        "kz" => "KAZAKHSTAN",
        "la" => "LAOS",
        "lb" => "LEBANON",
        "lc" => "SAINT LUCIA",
        "li" => "LIECHTENSTEIN",
        "lk" => "SRI LANKA",
        "lr" => "LIBERIA",
        "ls" => "LESOTHO",
        "lt" => "LITHUANIA",
        "lu" => "LUXEMBOURG",
        "lv" => "LATVIA",
        "ly" => "LIBYA",
        "ma" => "MOROCCO",
        "mc" => "MONACO",
        "md" => "MOLDOVA",
        "me" => "MONTENEGRO",
        "mf" => "SAINT MARTIN (FRENCH PART)",   
        "mg" => "MADAGASCAR",
        "mh" => "MARSHALL ISLANDS",
        "mk" => "REPUBLIC OF MACEDONIA",
        "ml" => "MALI",
        "mm" => "MYANMAR",
        "mn" => "MONGOLIA",
        "mo" => "MACAO",
        "mp" => "NORTHERN MARIANA ISLANDS",
        "mq" => "MARTINIQUE",
        "mr" => "MAURITANIA",
        "ms" => "MONTSERRAT",
        "mt" => "MALTA",
        "mu" => "MAURITIUS",
        "mv" => "MALDIVES",
        "mw" => "MALAWI",
        "mx" => "MEXICO",
        "my" => "MALAYSIA",
        "mz" => "MOZAMBIQUE",
        "na" => "NAMIBIA",
        "nc" => "NEW CALEDONIA",
        "ne" => "NIGER",
        "nf" => "NORFOLK ISLAND",
        "ng" => "NIGERIA",
        "ni" => "NICARAGUA",
        "nl" => "NETHERLANDS",
        "no" => "NORWAY",
        "np" => "NEPAL",
        "nr" => "NAURU",
        "nu" => "NIUE",
        "nz" => "NEW ZEALAND",
        "om" => "OMAN",
        "pa" => "PANAMA",
        "pe" => "PERU",
        "pf" => "FRENCH POLYNESIA",
        "pg" => "PAPUA NEW GUINEA",
        "ph" => "PHILIPPINES",
        "pk" => "PAKISTAN",
        "pl" => "POLAND",
        "pm" => "SAINT PIERRE AND MIQUELON",
        "pn" => "PITCAIRN ISLANDS",
        "pr" => "PUERTO RICO",
        "ps" => "PALESTINIAN TERRITORY, OCCUPIED",
        "pt" => "PORTUGAL",
        "pw" => "PALAU",
        "py" => "PARAGUAY",
        "qa" => "QATAR",
        "re" => "REUNION",
        "ro" => "ROMANIA",
        "rs" => "SERBIA",
        "ru" => "RUSSIA",
        "rw" => "RWANDA",
        "sa" => "SAUDI ARABIA",
        "sb" => "SOLOMON ISLANDS",
        "sc" => "SEYCHELLES",
        "sd" => "SUDAN",
        "se" => "SWEDEN",
        "sg" => "SINGAPORE",
        "sh" => "SAINT HELENA",
        "si" => "SLOVENIA",
        "sj" => "SVALBARD AND JAN MAYEN",
        "sk" => "SLOVAKIA",
        "sl" => "SIERRA LEONE",
        "sm" => "SAN MARINO",
        "sn" => "SENEGAL",
        "so" => "SOMALIA",
        "sr" => "SURINAME",
        "st" => "SAO TOME AND PRINCIPE",
        "su" => "RUSSIA", //ex-USSR, let's presume it's russia
        "sv" => "EL SALVADOR",
        "sx" => "SINT MAARTEN (DUTCH PART)",
        "sy" => "SYRIA",
        "sz" => "SWAZILAN",
        "tc" => "TURKS AND CAICOS ISLANDS",
        "td" => "CHAD",
        "tf" => "FRENCH SOUTHERN TERRITORIES",
        "tg" => "TOGO",
        "th" => "THAILAND",
        "tj" => "TAJIKISTAN",
        "tk" => "TOKELAU",
        "tl" => "EAST TIMOR",
        "tm" => "TURKMENISTAN",
        "tn" => "TUNISIA",
        "to" => "TONGA",
        "tp" => "EAST TIMOR",
        "tr" => "TURKEY",
        "tt" => "TRINIDAD AND TOBAGO",
        //"tv" => "TUVALU", don't include it here - it conflicts with .tv domain!
        "tw" => "TAIWAN",
        "tz" => "TANZANIA",
        "ua" => "UKRAINE",
        "ug" => "UGANDA",
        "uk" => "UNITED KINGDOM",
        "um" => "UNITED STATES MINOR OUTLYING ISLAND",
        "us" => "UNITED STATES",
        "uy" => "URUGUAY",
        "uz" => "UZBEKISTAN",
        "va" => "VATICAN CITY STATE",
        "vc" => "SAINT VINCENT AND THE GRENADINES",
        "ve" => "VENEZUELA",
        "vg" => "BRITISH VIRGIN ISLANDS",
        "vi" => "U.S. VIRGIN ISLANDS",
        "vn" => "VIETNAM",
        "vu" => "VANUATU",
        "wf" => "WALLIS AND FUTUNA",
        "ws" => "SAMOA",
        "ye" => "YEMEN",
        "yt" => "MAYOTTE",
        "yu" => "YUGOSLAVIA",
        "za" => "SOUTH AFRICA",
        "zm" => "ZAMBIA",
        "zw" => "ZIMBABWE"
        );

    $country = $countries[strtolower($country_code)];
    if ($country == "" && !$b_very_strict)
        $country = $country_code; //return at least something

    return $country;
}

最后,您可能会觉得使用以下功能对区域财富的国家/地区进行分类很方便:

function IsUserFromEuropeanUnion($country = "")
{
    if ($country == "")
        $country = GetCountryFromIP();

    if (strpos($country, "AUSTRIA") !== false ||
        strpos($country, "BELGIUM") !== false ||
        strpos($country, "BULGAR") !== false ||
        strpos($country, "CYPRUS") !== false ||
        strpos($country, "CZECH") !== false ||
        strpos($country, "DENMARK") !== false ||
        strpos($country, "ESTONIA") !== false ||
        strpos($country, "FINLAND") !== false ||
        strpos($country, "FRANCE") !== false ||
        strpos($country, "GERMANY") !== false ||
        strpos($country, "GREECE") !== false ||
        strpos($country, "HUNGARY") !== false ||
        strpos($country, "IRELAND") !== false ||
        strpos($country, "ITALY") !== false ||
        strpos($country, "LATVIA") !== false ||
        strpos($country, "LEICHTEN") !== false || //is it out of EU? anyway, it's reach and almost EU 
        strpos($country, "MONACO") !== false || //is it out of EU? anyway, it's reach and almost EU
        strpos($country, "NORWAY") !== false || //is it out of EU? anyway, it's reach and almost EU
        strpos($country, "SWITZERLANS") !== false || //is it out of EU? anyway, it's reach and almost EU
        strpos($country, "VATICAN") !== false || //is it out of EU? anyway, it's reach and almost EU
        strpos($country, "LITHUANIA") !== false ||
        strpos($country, "LUXEMBOURG") !== false ||
        strpos($country, "MALTA") !== false ||
        strpos($country, "NETHERL") !== false ||
        strpos($country, "POLAND") !== false ||
        strpos($country, "PORTUGAL") !== false ||
        strpos($country, "ROMANIA") !== false ||
        strpos($country, "SLOVAKIA") !== false ||
        strpos($country, "SLOVENIA") !== false ||
        strpos($country, "SPAIN") !== false ||
        strpos($country, "SWEDEN") !== false ||
        strpos($country, "IRELAND") !== false ||
        strpos($country, "UNITED KINGDOM") !== false)
        return true;
    else
        return false;
}

function IsUserFromEurope($country = "")
{
    if (IsUserFromEuropeanUnion($country) == true)
        return true;

    if ($country == "")
        $country = GetCountryFromIP();

    if (strpos($country, "ALBANIA") !== false ||
        strpos($country, "ANDORRA") !== false ||
        //strpos($country, "ARMENIA") !== false ||
        //strpos($country, "AZERBAI") !== false ||
        strpos($country, "BELARUS") !== false ||
        strpos($country, "BOSNIA") !== false ||
        strpos($country, "HERZE") !== false ||
        strpos($country, "CROATIA") !== false ||
        strpos($country, "BULGARIA") !== false ||
        strpos($country, "CYPRUS") !== false ||
        strpos($country, "ICELAND") !== false ||
        strpos($country, "IRELAND") !== false ||
        strpos($country, "MACEDONIA") !== false ||
        strpos($country, "MALTA") !== false ||
        strpos($country, "MOLDOVA") !== false ||
        strpos($country, "MONTENEGRO") !== false ||
        strpos($country, "SAN MARINO") !== false ||
        strpos($country, "UKRAINE") !== false ||
        strpos($country, "RUSSIA") !== false ||
        strpos($country, "GEORGIA") !== false ||
        strpos($country, "SERBIA") !== false)
        return TRUE;
    else
        return FALSE;
}

function IsUserFromUSorCanada($country = "")
{
    if ($country == "")
        $country = GetCountryFromIP();

    if (strpos($country, "UNITED STATES") !== false || 
        strpos($country, "NAVASSA") !== false || //USA
        strpos($country, "PUERTO RICCO") !== false || //USA
        strpos($country, "PUERTORICCO") !== false || //USA
        strpos($country, "S VIRGIN ISLANDS") !== false || //USA
        strpos($country, "S. VIRGIN ISLANDS") !== false || //USA
        strpos($country, "SAMOA") !== false || //USA
        strpos($country, "GUAM") !== false || //USA
        strpos($country, "MARIANA ISLAND") !== false || //USA
        strpos($country, "WAKE ISLAND") !== false || //USA
        strpos($country, "HAWAII") !== false || //USA
        strpos($country, "CANADA") !== false)
        return TRUE;
    else
        return FALSE;
}

function IsUserFromNorthAmerica($country = "")
{
    if ($country == "")
        $country = GetCountryFromIP();

    if (IsUserFromUSorCanada($country) == true)
        return true;

    if (strpos($country, "BERMUD") !== false ||
        strpos($country, "GREENLAND") !== false ||
        strpos($country, "MEXICO") !== false ||
        strpos($country, "SAINT PIERRE") !== false ||
        //Carribean countries:
        strpos($country, "ANGUILLA") !== false ||
        strpos($country, "ANTIGUA") !== false ||
        strpos($country, "BARBUDA") !== false ||
        strpos($country, "BAHAMAS") !== false ||
        strpos($country, "BARBADOS") !== false ||
        strpos($country, "VIRGIN ISL") !== false ||
        strpos($country, "CANYMAN") !== false ||
        strpos($country, "CUBA") !== false ||
        strpos($country, "DOMINICA") !== false ||
        strpos($country, "DOMINICAN") !== false ||
        strpos($country, "GRENADA") !== false ||
        strpos($country, "GUADELOUPE") !== false ||
        strpos($country, "HAITI") !== false ||
        strpos($country, "JAMAICA") !== false ||
        strpos($country, "MARTINIQUE") !== false ||
        strpos($country, "MONTSERRAT") !== false ||
        strpos($country, "NAVASSA") !== false || //USA
        strpos($country, "ANTILLES") !== false ||
        strpos($country, "PUERTO RICCO") !== false || //USA
        strpos($country, "PUERTORICCO") !== false || //USA
        strpos($country, "SAINT BART") !== false ||
        strpos($country, "SAINT KITTS") !== false ||
        strpos($country, "SAINT LUCIA") !== false ||
        strpos($country, "SAINT MARTIN") !== false ||
        strpos($country, "SAINT VINCENT") !== false ||
        strpos($country, "TRINIDAD") !== false ||
        strpos($country, "TOBAGO") !== false ||
        strpos($country, "TURKS AND CAICOS") !== false ||
        strpos($country, "S VIRGIN ISLANDS") !== false || //USA
        //Cental America:
        strpos($country, "BELIZE") !== false ||
        strpos($country, "COSTA RICA") !== false ||
        strpos($country, "SALVADOR") !== false ||
        strpos($country, "GUATEMALA") !== false ||
        strpos($country, "HONDURAS") !== false ||
        strpos($country, "NICARAGUA") !== false ||
        strpos($country, "PANAMA") !== false)
        return true;
    else
        return false;
}

function IsUserFromSouthAmerica($country = "")
{
    if ($country == "")
        $country = GetCountryFromIP();

    if (strpos($country, "ARGENTINA") !== false ||
        strpos($country, "BOLIVIA") !== false ||
        strpos($country, "BRAZIL") !== false ||
        strpos($country, "CHILE") !== false ||
        strpos($country, "COLOMBIA") !== false ||
        strpos($country, "ECUADOR") !== false ||
        strpos($country, "FALKLAND") !== false ||
        strpos($country, "FRENCH GUIANA") !== false ||
        strpos($country, "GUYANA") !== false ||
        strpos($country, "PARAGUAY") !== false ||
        strpos($country, "PERU") !== false ||
        strpos($country, "SOUTH GEORGIA") !== false ||
        strpos($country, "SANDWICH ISL") !== false ||
        strpos($country, "SURINAME") !== false ||
        strpos($country, "URUGUAY") !== false ||
        strpos($country, "VENEZUELLA") !== false)
        return true;
    else
        return false;
}

function IsUserFromAsia($country = "")
{
    if ($country == "")
        $country = GetCountryFromIP();

    if ( strpos($country, "PAKISTAN") !== false ||
         strpos($country, "VIETNAM") !== false ||
         strpos($country, "VIET NAM") !== false ||
         strpos($country, "THAILAND") !== false ||
         strpos($country, "SINGAPORE") !== false ||
         strpos($country, "SAUDI") !== false ||
         strpos($country, "ARABI") !== false ||
         strpos($country, "BANGLADESH") !== false ||
         strpos($country, "KOREA") !== false ||
         strpos($country, "HONG") !== false ||
         strpos($country, "MACAU") !== false ||
         strpos($country, "MONGOL") !== false ||
         strpos($country, "KOREA") !== false ||
         strpos($country, "BRUNE") !== false ||
         strpos($country, "BURMA") !== false ||
         strpos($country, "CAMBODIA") !== false ||
         strpos($country, "TIMOR") !== false ||
         strpos($country, "LAOS") !== false ||
         strpos($country, "MALAYSIA") !== false ||
         strpos($country, "PHILIPPINES") !== false ||
         strpos($country, "SINGAPORE") !== false ||
         strpos($country, "THAILAND") !== false ||
         strpos($country, "VIETNAM") !== false ||
         strpos($country, "VIET NAM") !== false ||
         strpos($country, "AFGHANI") !== false ||
         strpos($country, "PAKISTAN") !== false ||
         strpos($country, "BANGLADESH") !== false ||
         strpos($country, "BHUTAN") !== false ||
         strpos($country, "MALDIVES") !== false ||
         strpos($country, "NEPAL") !== false ||
         strpos($country, "SRI LANKA") !== false ||
         strpos($country, "SRILANKA") !== false ||
         strpos($country, "BAHRAIN") !== false ||
         strpos($country, "IRAQ") !== false ||
         strpos($country, "IRAN") !== false ||
         strpos($country, "ISRAEL") !== false ||
         strpos($country, "JORDAN") !== false ||
         strpos($country, "KUWAIT") !== false ||
         strpos($country, "LEBANON") !== false ||
         strpos($country, "OMAN") !== false ||
         strpos($country, "PALESTIN") !== false ||
         strpos($country, "QATAR") !== false ||
         strpos($country, "SAUDI") !== false ||
         strpos($country, "ARABI") !== false ||
         strpos($country, "SYRIA") !== false ||
         strpos($country, "TURKEY") !== false ||
         strpos($country, "EMIRATES") !== false ||
         strpos($country, "YEMEN") !== false ||
         strpos($country, "ARMENIA") !== false ||
         strpos($country, "AZERBAIJAN") !== false ||
         strpos($country, "KAZAKHSTAN") !== false ||
         strpos($country, "KYRGYZ") !== false ||
         strpos($country, "TAJIKI") !== false ||
         strpos($country, "TURKMENI") !== false ||
         strpos($country, "UZBEKI") !== false)
        return true;
    else
        return false;
}

function IsUserFromVeryRichCountry($country = "")
{
    //List of economy index is taken from there: https://www.cia.gov/library/publications/the-world-factbook/rankorder/2004rank.html
    if ($country == "")
        $country = GetCountryFromIP();

    //note: top of this list is the richest
    if (strpos($country, "LIECHTENSTEIN") !== false ||
        strpos($country, "QATAR") !== false ||
        strpos($country, "LUXEMBOURG") !== false ||
        strpos($country, "BERMUDA") !== false ||
        strpos($country, "NORWAY") !== false ||
        strpos($country, "JERSEY") !== false ||
        strpos($country, "KUWAIT") !== false ||
        strpos($country, "SINGAPORE") !== false ||
        strpos($country, "BRUNEI") !== false ||
        strpos($country, "FAROE ISLANDS") !== false)
        return true;
    else
        return false;
}

function IsUserFromQuiteRichCountry($country = "")
{
    //List of economy index is taken from there: https://www.cia.gov/library/publications/the-world-factbook/rankorder/2004rank.html
    if ($country == "")
        $country = GetCountryFromIP();

    //note: top of this list is the richest
    if (strpos($country, "UNITED STATES") !== false ||
        strpos($country, "ANDORRA") !== false ||
        strpos($country, "GUERNSEY") !== false ||
        strpos($country, "CAYMAN ISLANDS") !== false ||
        strpos($country, "HONG KONG") !== false ||
        strpos($country, "SAN MARINO") !== false ||
        strpos($country, "SWITZERLAND") !== false ||
        strpos($country, "IRELAND") !== false ||
        strpos($country, "AUSTRALIA") !== false ||
        strpos($country, "ICELAND") !== false ||
        strpos($country, "NETHERLANDS") !== false ||
        strpos($country, "AUSTRIA") !== false ||
        strpos($country, "UNITED ARAB EMIRATES") !== false ||
        strpos($country, "BAHRAIN") !== false ||
        strpos($country, "BRITISH VIRGIN ISLANDS") !== false ||
        strpos($country, "GIBRALTAR") !== false ||
        strpos($country, "CANADA") !== false ||
        strpos($country, "EQUATORIAL GUINEA") !== false ||
        strpos($country, "BELGIUM") !== false ||
        strpos($country, "SWEDEN") !== false ||
        strpos($country, "DENMARK") !== false ||
        strpos($country, "GREENLAND") !== false ||
        strpos($country, "FALKLAND ISLANDS (ISLAS MALVINAS)") !== false ||
        strpos($country, "ISLE OF MAN") !== false ||
        strpos($country, "UNITED KINGDOM") !== false ||
        strpos($country, "FINLAND") !== false ||
        strpos($country, "GERMANY") !== false ||
        strpos($country, "SPAIN") !== false ||
        strpos($country, "MACAU") !== false ||
        strpos($country, "JAPAN") !== false ||
        strpos($country, "FRANCE") !== false ||
        strpos($country, "EUROPEAN UNION") !== false ||
        strpos($country, "TAIWAN") !== false ||
        strpos($country, "GREECE") !== false ||
        strpos($country, "MONACO") !== false ||
        strpos($country, "ITALY") !== false)
        return true;
    else
        return false;
}

答案 1 :(得分:2)

你基本上都在问:我有车,是否有可能在没有启动或转向的情况下将车开到某个地方?

如果您有数据库,请使用它。它们可以帮助检索/排序数据。其他任何事情都可能更慢/更不可靠。

如果你已经设置了它,那么将数据库转储到一个文件中,并为搜索脚本处理的每个请求加载/解析它。

答案 2 :(得分:1)

如果您不想使用数据库,可以使用gethostbyaddr

$country = substr(strrchr(gethostbyaddr($_SERVER['REMOTE_ADDR']), '.'), 1);

此处$country可能包含deuk等值,通常此值类似于comnet这不代表一个国家。

此外gethostbyaddr可能会很慢..

所以我会使用数据库。

答案 3 :(得分:0)

没有。 IP地址与国家/地区之间没有任何关系。

如果您想将所有数据放入数组中,那么您就不必连接到数据库了。但这会导致文件很大。

答案 4 :(得分:0)

您可以使用第三方API。

Google中出现的第一个:

http://ip2country.hackers.lv/

示例:http://ip2country.hackers.lv/api/ip2country?ip=11.12.13.14

答案 5 :(得分:0)

嗯...我看到答案“这是不可能的”,所以只是为了表明它是可能的,它将像DB一样快速工作,我会把这个php级: http://www.filefactory.com/file/cee5cb6/n/sypexgeo.zip

这不是我的代码,你会在评论中看到作者。 作者不再支持该库,并且无法定义某些IP。 如果您将文件SypexGeo.dat的内容放入缓存(322Kb),它将比DB更快地工作,但是对于DB,您可以轻松更新新IP的列表。

答案 6 :(得分:0)

这个类使用基于文本的数据库,你可以使用它

http://www.phpclasses.org/package/2363-PHP-Lookup-the-country-of-a-given-IP-address.html

或者这里使用cvs数据库

http://www.phpclasses.org/package/1477-PHP-Determine-the-country-of-a-given-IP-address.html

答案 7 :(得分:0)

IP2CountryWoDb PHP是一个用于IP国家定义的脚本,不使用BD和网络查询。 (俄语网页)

PS这篇帖子是我用机器翻译写的,很抱歉可能存在差异

答案 8 :(得分:0)

您可以在https://github.com/ip2iq/ip2-lib-php尝试IP2IQ IP2 PHP库,该库在传递IPv4地址时返回国家/地区代码。不需要SQL数据库,这是我们在开发过程中想要的功能,它比几乎任何东西都快。

唯一的问题是,现在您必须保留一个关联数组,以将返回的国家/地区代码映射到国家/地区名称,该名称可能因语言而异。您可以使用类似https://gist.github.com/DHS/1340150的内容来执行此操作。

答案 9 :(得分:-1)

我希望到现在为止你必须找到解决这个问题的方法,否则,请看看:

http://detectdevice.com/

至少就目前而言,这对我有用。