• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

PHP如何使用GeoIP数据库

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1、首先下载GeoIP的IP库。参考《利用GeoIP数据库及API进行地理定位查询》。下载后解压,得到一个GeoIP.dat文件

2、新建一个文件geoip.inc。内容为

   1 <?php
   2 
   3 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
   4 /* geoip.inc
   5  *
   6  * Copyright (C) 2007 MaxMind LLC
   7  *
   8  * This library is free software; you can redistribute it and/or
   9  * modify it under the terms of the GNU Lesser General Public
  10  * License as published by the Free Software Foundation; either
  11  * version 2.1 of the License, or (at your option) any later version.
  12  *
  13  * This library is distributed in the hope that it will be useful,
  14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16  * Lesser General Public License for more details.
  17  *
  18  * You should have received a copy of the GNU Lesser General Public
  19  * License along with this library; if not, write to the Free Software
  20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21  */
  22 
  23 define("GEOIP_COUNTRY_BEGIN", 16776960);
  24 define("GEOIP_STATE_BEGIN_REV0", 16700000);
  25 define("GEOIP_STATE_BEGIN_REV1", 16000000);
  26 define("GEOIP_STANDARD", 0);
  27 define("GEOIP_MEMORY_CACHE", 1);
  28 define("GEOIP_SHARED_MEMORY", 2);
  29 define("STRUCTURE_INFO_MAX_SIZE", 20);
  30 define("DATABASE_INFO_MAX_SIZE", 100);
  31 define("GEOIP_COUNTRY_EDITION", 1);
  32 define("GEOIP_PROXY_EDITION", 8);
  33 define("GEOIP_ASNUM_EDITION", 9);
  34 define("GEOIP_NETSPEED_EDITION", 10);
  35 define("GEOIP_REGION_EDITION_REV0", 7);
  36 define("GEOIP_REGION_EDITION_REV1", 3);
  37 define("GEOIP_CITY_EDITION_REV0", 6);
  38 define("GEOIP_CITY_EDITION_REV1", 2);
  39 define("GEOIP_ORG_EDITION", 5);
  40 define("GEOIP_ISP_EDITION", 4);
  41 define("SEGMENT_RECORD_LENGTH", 3);
  42 define("STANDARD_RECORD_LENGTH", 3);
  43 define("ORG_RECORD_LENGTH", 4);
  44 define("MAX_RECORD_LENGTH", 4);
  45 define("MAX_ORG_RECORD_LENGTH", 300);
  46 define("GEOIP_SHM_KEY", 0x4f415401);
  47 define("US_OFFSET", 1);
  48 define("CANADA_OFFSET", 677);
  49 define("WORLD_OFFSET", 1353);
  50 define("FIPS_RANGE", 360);
  51 define("GEOIP_UNKNOWN_SPEED", 0);
  52 define("GEOIP_DIALUP_SPEED", 1);
  53 define("GEOIP_CABLEDSL_SPEED", 2);
  54 define("GEOIP_CORPORATE_SPEED", 3);
  55 define("GEOIP_DOMAIN_EDITION", 11);
  56 define("GEOIP_COUNTRY_EDITION_V6", 12);
  57 define("GEOIP_LOCATIONA_EDITION", 13);
  58 define("GEOIP_ACCURACYRADIUS_EDITION", 14);
  59 define("GEOIP_CITYCOMBINED_EDITION", 15);
  60 define("GEOIP_CITY_EDITION_REV1_V6", 30);
  61 define("GEOIP_CITY_EDITION_REV0_V6", 31);
  62 define("GEOIP_NETSPEED_EDITION_REV1", 32);
  63 define("GEOIP_NETSPEED_EDITION_REV1_V6", 33);
  64 define("GEOIP_USERTYPE_EDITION", 28);
  65 define("GEOIP_USERTYPE_EDITION_V6", 29);
  66 define("GEOIP_ASNUM_EDITION_V6", 21);
  67 define("GEOIP_ISP_EDITION_V6", 22);
  68 define("GEOIP_ORG_EDITION_V6", 23);
  69 define("GEOIP_DOMAIN_EDITION_V6", 24);
  70 
  71 define("CITYCOMBINED_FIXED_RECORD", 7);
  72 
  73 class GeoIP
  74 {
  75     public $flags;
  76     public $filehandle;
  77     public $memory_buffer;
  78     public $databaseType;
  79     public $databaseSegments;
  80     public $record_length;
  81     public $shmid;
  82     public $GEOIP_COUNTRY_CODE_TO_NUMBER = array(
  83         "" => 0,
  84         "AP" => 1,
  85         "EU" => 2,
  86         "AD" => 3,
  87         "AE" => 4,
  88         "AF" => 5,
  89         "AG" => 6,
  90         "AI" => 7,
  91         "AL" => 8,
  92         "AM" => 9,
  93         "CW" => 10,
  94         "AO" => 11,
  95         "AQ" => 12,
  96         "AR" => 13,
  97         "AS" => 14,
  98         "AT" => 15,
  99         "AU" => 16,
 100         "AW" => 17,
 101         "AZ" => 18,
 102         "BA" => 19,
 103         "BB" => 20,
 104         "BD" => 21,
 105         "BE" => 22,
 106         "BF" => 23,
 107         "BG" => 24,
 108         "BH" => 25,
 109         "BI" => 26,
 110         "BJ" => 27,
 111         "BM" => 28,
 112         "BN" => 29,
 113         "BO" => 30,
 114         "BR" => 31,
 115         "BS" => 32,
 116         "BT" => 33,
 117         "BV" => 34,
 118         "BW" => 35,
 119         "BY" => 36,
 120         "BZ" => 37,
 121         "CA" => 38,
 122         "CC" => 39,
 123         "CD" => 40,
 124         "CF" => 41,
 125         "CG" => 42,
 126         "CH" => 43,
 127         "CI" => 44,
 128         "CK" => 45,
 129         "CL" => 46,
 130         "CM" => 47,
 131         "CN" => 48,
 132         "CO" => 49,
 133         "CR" => 50,
 134         "CU" => 51,
 135         "CV" => 52,
 136         "CX" => 53,
 137         "CY" => 54,
 138         "CZ" => 55,
 139         "DE" => 56,
 140         "DJ" => 57,
 141         "DK" => 58,
 142         "DM" => 59,
 143         "DO" => 60,
 144         "DZ" => 61,
 145         "EC" => 62,
 146         "EE" => 63,
 147         "EG" => 64,
 148         "EH" => 65,
 149         "ER" => 66,
 150         "ES" => 67,
 151         "ET" => 68,
 152         "FI" => 69,
 153         "FJ" => 70,
 154         "FK" => 71,
 155         "FM" => 72,
 156         "FO" => 73,
 157         "FR" => 74,
 158         "SX" => 75,
 159         "GA" => 76,
 160         "GB" => 77,
 161         "GD" => 78,
 162         "GE" => 79,
 163         "GF" => 80,
 164         "GH" => 81,
 165         "GI" => 82,
 166         "GL" => 83,
 167         "GM" => 84,
 168         "GN" => 85,
 169         "GP" => 86,
 170         "GQ" => 87,
 171         "GR" => 88,
 172         "GS" => 89,
 173         "GT" => 90,
 174         "GU" => 91,
 175         "GW" => 92,
 176         "GY" => 93,
 177         "HK" => 94,
 178         "HM" => 95,
 179         "HN" => 96,
 180         "HR" => 97,
 181         "HT" => 98,
 182         "HU" => 99,
 183         "ID" => 100,
 184         "IE" => 101,
 185         "IL" => 102,
 186         "IN" => 103,
 187         "IO" => 104,
 188         "IQ" => 105,
 189         "IR" => 106,
 190         "IS" => 107,
 191         "IT" => 108,
 192         "JM" => 109,
 193         "JO" => 110,
 194         "JP" => 111,
 195         "KE" => 112,
 196         "KG" => 113,
 197         "KH" => 114,
 198         "KI" => 115,
 199         "KM" => 116,
 200         "KN" => 117,
 201         "KP" => 118,
 202         "KR" => 119,
 203         "KW" => 120,
 204         "KY" => 121,
 205         "KZ" => 122,
 206         "LA" => 123,
 207         "LB" => 124,
 208         "LC" => 125,
 209         "LI" => 126,
 210         "LK" => 127,
 211         "LR" => 128,
 212         "LS" => 129,
 213         "LT" => 130,
 214         "LU" => 131,
 215         "LV" => 132,
 216         "LY" => 133,
 217         "MA" => 134,
 218         "MC" => 135,
 219         "MD" => 136,
 220         "MG" => 137,
 221         "MH" => 138,
 222         "MK" => 139,
 223         "ML" => 140,
 224         "MM" => 141,
 225         "MN" => 142,
 226         "MO" => 143,
 227         "MP" => 144,
 228         "MQ" => 145,
 229         "MR" => 146,
 230         "MS" => 147,
 231         "MT" => 148,
 232         "MU" => 149,
 233         "MV" => 150,
 234         "MW" => 151,
 235         "MX" => 152,
 236         "MY" => 153,
 237         "MZ" => 154,
 238         "NA" => 155,
 239         "NC" => 156,
 240         "NE" => 157,
 241         "NF" => 158,
 242         "NG" => 159,
 243         "NI" => 160,
 244         "NL" => 161,
 245         "NO" => 162,
 246         "NP" => 163,
 247         "NR" => 164,
 248         "NU" => 165,
 249         "NZ" => 166,
 250         "OM" => 167,
 251         "PA" => 168,
 252         "PE" => 169,
 253         "PF" => 170,
 254         "PG" => 171,
 255         "PH" => 172,
 256         "PK" => 173,
 257         "PL" => 174,
 258         "PM" => 175,
 259         "PN" => 176,
 260         "PR" => 177,
 261         "PS" => 178,
 262         "PT" => 179,
 263         "PW" => 180,
 264         "PY" => 181,
 265         "QA" => 182,
 266         "RE" => 183,
 267         "RO" => 184,
 268         "RU" => 185,
 269         "RW" => 186,
 270         "SA" => 187,
 271         "SB" => 188,
 272         "SC" => 189,
 273         "SD" => 190,
 274         "SE" => 191,
 275         "SG" => 192,
 276         "SH" => 193,
 277         "SI" => 194,
 278         "SJ" => 195,
 279         "SK" => 196,
 280         "SL" => 197,
 281         "SM" => 198,
 282         "SN" => 199,
 283         "SO" => 200,
 284         "SR" => 201,
 285         "ST" => 202,
 286         "SV" => 203,
 287         "SY" => 204,
 288         "SZ" => 205,
 289         "TC" => 206,
 290         "TD" => 207,
 291         "TF" => 208,
 292         "TG" => 209,
 293         "TH" => 210,
 294         "TJ" => 211,
 295         "TK" => 212,
 296         "TM" => 213,
 297         "TN" => 214,
 298         "TO" => 215,
 299         "TL" => 216,
 300         "TR" => 217,
 301         "TT" => 218,
 302         "TV" => 219,
 303         "TW" => 220,
 304         "TZ" => 221,
 305         "UA" => 222,
 306         "UG" => 223,
 307         "UM" => 224,
 308         "US" => 225,
 309         "UY" => 226,
 310         "UZ" => 227,
 311         "VA" => 228,
 312         "VC" => 229,
 313         "VE" => 230,
 314         "VG" => 231,
 315         "VI" => 232,
 316         "VN" => 233,
 317         "VU" => 234,
 318         "WF" => 235,
 319         "WS" => 236,
 320         "YE" => 237,
 321         "YT" => 238,
 322         "RS" => 239,
 323         "ZA" => 240,
 324         "ZM" => 241,
 325         "ME" => 242,
 326         "ZW" => 243,
 327         "A1" => 244,
 328         "A2" => 245,
 329         "O1" => 246,
 330         "AX" => 247,
 331         "GG" => 248,
 332         "IM" => 249,
 333         "JE" => 250,
 334         "BL" => 251,
 335         "MF" => 252,
 336         "BQ" => 253,
 337         "SS" => 254
 338     );
 339 
 340     public $GEOIP_COUNTRY_CODES = array(
 341         "",
 342         "AP",
 343         "EU",
 344         "AD",
 345         "AE",
 346         "AF",
 347         "AG",
 348         "AI",
 349         "AL",
 350         "AM",
 351         "CW",
 352         "AO",
 353         "AQ",
 354         "AR",
 355         "AS",
 356         "AT",
 357         "AU",
 358         "AW",
 359         "AZ",
 360         "BA",
 361         "BB",
 362         "BD",
 363         "BE",
 364         "BF",
 365         "BG",
 366         "BH",
 367         "BI",
 368         "BJ",
 369         "BM",
 370         "BN",
 371         "BO",
 372         "BR",
 373         "BS",
 374         "BT",
 375         "BV",
 376         "BW",
 377         "BY",
 378         "BZ",
 379         "CA",
 380         "CC",
 381         "CD",
 382         "CF",
 383         "CG",
 384         "CH",
 385         "CI",
 386         "CK",
 387         "CL",
 388         "CM",
 389         "CN",
 390         "CO",
 391         "CR",
 392         "CU",
 393         "CV",
 394         "CX",
 395         "CY",
 396         "CZ",
 397         "DE",
 398         "DJ",
 399         "DK",
 400         "DM",
 401         "DO",
 402         "DZ",
 403         "EC",
 404         "EE",
 405         "EG",
 406         "EH",
 407         "ER",
 408         "ES",
 409         "ET",
 410         "FI",
 411         "FJ",
 412         "FK",
 413         "FM",
 414         "FO",
 415         "FR",
 416         "SX",
 417         "GA",
 418         "GB",
 419         "GD",
 420         "GE",
 421         "GF",
 422         "GH",
 423         "GI",
 424         "GL",
 425         "GM",
 426         "GN",
 427         "GP",
 428         "GQ",
 429         "GR",
 430         "GS",
 431         "GT",
 432         "GU",
 433         "GW",
 434         "GY",
 435         "HK",
 436         "HM",
 437         "HN",
 438         "HR",
 439         "HT",
 440         "HU",
 441         "ID",
 442         "IE",
 443         "IL",
 444         "IN",
 445         "IO",
 446         "IQ",
 447         "IR",
 448         "IS",
 449         "IT",
 450         "JM",
 451         "JO",
 452         "JP",
 453         "KE",
 454         "KG",
 455         "KH",
 456         "KI",
 457         "KM",
 458         "KN",
 459         "KP",
 460         "KR",
 461         "KW",
 462         "KY",
 463         "KZ",
 464         "LA",
 465         "LB",
 466         "LC",
 467         "LI",
 468         "LK",
 469         "LR",
 470         "LS",
 471         "LT",
 472         "LU",
 473         "LV",
 474         "LY",
 475         "MA",
 476         "MC",
 477         "MD",
 478         "MG",
 479         "MH",
 480         "MK",
 481         "ML",
 482         "MM",
 483         "MN",
 484         "MO",
 485         "MP",
 486         "MQ",
 487         "MR",
 488         "MS",
 489         "MT",
 490         "MU",
 491         "MV",
 492         "MW",
 493         "MX",
 494         "MY",
 495         "MZ",
 496         "NA",
 497         "NC",
 498         "NE",
 499         "NF",
 500         "NG",
 501         "NI",
 502         "NL",
 503         "NO",
 504         "NP",
 505         "NR",
 506         "NU",
 507         "NZ",
 508         "OM",
 509         "PA",
 510         "PE",
 511         "PF",
 512         "PG",
 513         "PH",
 514         "PK",
 515         "PL",
 516         "PM",
 517         "PN",
 518         "PR",
 519         "PS",
 520         "PT",
 521         "PW",
 522         "PY",
 523         "QA",
 524         "RE",
 525         "RO",
 526         "RU",
 527         "RW",
 528         "SA",
 529         "SB",
 530         "SC",
 531         "SD",
 532         "SE",
 533         "SG",
 534         "SH",
 535         "SI",
 536         "SJ",
 537         "SK",
 538         "SL",
 539         "SM",
 540         "SN",
 541         "SO",
 542         "SR",
 543         "ST",
 544         "SV",
 545         "SY",
 546         "SZ",
 547         "TC",
 548         "TD",
 549         "TF",
 550         "TG",
 551         "TH",
 552         "TJ",
 553         "TK",
 554         "TM",
 555         "TN",
 556         "TO",
 557         "TL",
 558         "TR",
 559         "TT",
 560         "TV",
 561         "TW",
 562         "TZ",
 563         "UA",
 564         "UG",
 565         "UM",
 566         "US",
 567         "UY",
 568         "UZ",
 569         "VA",
 570         "VC",
 571         "VE",
 572         "VG",
 573         "VI",
 574         "VN",
 575         "VU",
 576         "WF",
 577         "WS",
 578         "YE",
 579         "YT",
 580         "RS",
 581         "ZA",
 582         "ZM",
 583         "ME",
 584         "ZW",
 585         "A1",
 586         "A2",
 587         "O1",
 588         "AX",
 589         "GG",
 590         "IM",
 591         "JE",
 592         "BL",
 593         "MF",
 594         "BQ",
 595         "SS",
 596         "O1"
 597     );
 598 
 599     public $GEOIP_COUNTRY_CODES3 = array(
 600         "",
 601         "AP",
 602         "EU",
 603         "AND",
 604         "ARE",
 605         "AFG",
 606         "ATG",
 607         "AIA",
 608         "ALB",
 609         "ARM",
 610         "CUW",
 611         "AGO",
 612         "ATA",
 613         "ARG",
 614         "ASM",
 615         "AUT",
 616         "AUS",
 617         "ABW",
 618         "AZE",
 619         "BIH",
 620         "BRB",
 621         "BGD",
 622         "BEL",
 623         "BFA",
 624         "BGR",
 625         "BHR",
 626         "BDI",
 627         "BEN",
 628         "BMU",
 629         "BRN",
 630         "BOL",
 631         "BRA",
 632         "BHS",
 633         "BTN",
 634         "BVT",
 635         "BWA",
 636         "BLR",
 637         "BLZ",
 638         "CAN",
 639         "CCK",
 640         "COD",
 641         "CAF",
 642         "COG",
 643         "CHE",
 644         "CIV",
 645         "COK",
 646         "CHL",
 647         "CMR",
 648         "CHN",
 649         "COL",
 650         "CRI",
 651         "CUB",
 652         "CPV",
 653         "CXR",
 654         "CYP",
 655         "CZE",
 656         "DEU",
 657         "DJI",
 658         "DNK",
 659         "DMA",
 660         "DOM",
 661         "DZA",
 662         "ECU",
 663         "EST",
 664         "EGY",
 665         "ESH",
 666         "ERI",
 667         "ESP",
 668         "ETH",
 669         "FIN",
 670         "FJI",
 671         "FLK",
 672         "FSM",
 673         "FRO",
 674         "FRA",
 675         "SXM",
 676         "GAB",
 677         "GBR",
 678         "GRD",
 679         "GEO",
 680         "GUF",
 681         "GHA",
 682         "GIB",
 683         "GRL",
 684         "GMB",
 685         "GIN",
 686         "GLP",
 687         "GNQ",
 688         "GRC",
 689         "SGS",
 690         "GTM",
 691         "GUM",
 692         "GNB",
 693         "GUY",
 694         "HKG",
 695         "HMD",
 696         "HND",
 697         "HRV",
 698         "HTI",
 699         "HUN",
 700         "IDN",
 701         "IRL",
 702         "ISR",
 703         "IND",
 704         "IOT",
 705         "IRQ",
 706         "IRN",
 707         "ISL",
 708         "ITA",
 709         "JAM",
 710         "JOR",
 711         "JPN",
 712         "KEN",
 713         "KGZ",
 714         "KHM",
 715         "KIR",
 716         "COM",
 717         "KNA",
 718         "PRK",
 719         "KOR",
 720         "KWT",
 721         "CYM",
 722         "KAZ",
 723         "LAO",
 724         "LBN",
 725         "LCA",
 726         "LIE",
 727         "LKA",
 728         "LBR",
 729         "LSO",
 730         "LTU",
 731         "LUX",
 732         "LVA",
 733         "LBY",
 734         "MAR",
 735         "MCO",
 736         "MDA",
 737         "MDG",
 738         "MHL",
 739         "MKD",
 740         "MLI",
 741         "MMR",
 742         "MNG",
 743         "MAC",
 744         "MNP",
 745         "MTQ",
 746         "MRT",
 747         "MSR",
 748         "MLT",
 749         "MUS",
 750         "MDV",
 751         "MWI",
 752         "MEX",
 753         "MYS",
 754         "MOZ",
 755         "NAM",
 756         "NCL",
 757         "NER",
 758         "NFK",
 759         "NGA",
 760         "NIC",
 761         "NLD",
 762         "NOR",
 763         "NPL",
 764         "NRU",
 765         "NIU",
 766         "NZL",
 767         "OMN",
 768         "PAN",
 769         "PER",
 770         "PYF",
 771         "PNG",
 772         "PHL",
 773         "PAK",
 774         "POL",
 775         "SPM",
 776         "PCN",
 777         "PRI",
 778         "PSE",
 779         "PRT",
 780         "PLW",
 781         "PRY" 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php编译安装参数详解发布时间:2022-07-10
下一篇:
初识PHP发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap