PHP获取IP所在地区(转)

1.获取IP地址的API
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
新浪多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42

搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson
搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8

有道xml

http://www.youdao.com/smartresult-xml/search.s?type=ip&q=115.156.238.114

淘宝IP地址库API http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]

根据访问者ip显示对应地区的特定内容

根据访问者ip,来读ip库或接口获取地区,用该地区做筛选查询

纯真IP数据库文件版 http://www.cz88.net/fox/

示例一:

1 $IpLocation = new IpLocation();
2 $client = $IpLocation->getlocation();
3 print_r($client);

示例二:

1 header("Content-type: text/html; charset=gbk"); 
2 $IpLocation = new IpLocation('../qqwry/QQWry.Dat');
3 $client = $IpLocation->getlocation('115.148.101.72');
4 print_r($client);

相关说明

ShowIP.exe可以验证程序,也可以更新。 此类要求提供纯真IP数据库,附件包里包含有此数据库

如果电脑配置不好出现文件太大内存溢出问题,可以将ip库换成mysql版

使用IPLook把QQWry.Dat文件解压到IPData.txt文件,打开IPData.txt文件格式是这样的

    0.0.0.0    0.255.255.255    IANA保留地址    CZ88.NET
    1.0.0.0    1.0.0.255    澳大利亚    CZ88.NET
    1.0.1.0    1.0.3.255    福建省    电信
    1.0.4.0    1.0.7.255    澳大利亚    CZ88.NET

建立ip数据库表

1 CREATE TABLE `ip` (
2 `StartIP` varchar(20) default '',
3 `EndIP` varchar(20) default NULL,
4 `Country` varchar(30) default NULL,
5 `Local` varchar(50) default NULL,
6 KEY `IP` USING BTREE (`StartIP`,`EndIP`)
7 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

MySQL的导入向导功能把IPData.txt文件的数据导入到ip数据表中

load data local infile 'data.txt'  into 表名  (列名)

如何在MySQL中对ip比较

select  *  from ip where INET_ATON('119.253.32.42') between INET_ATON(startIp) and INET_ATON(endIp);
原文地址:https://www.cnblogs.com/xingmeng/p/3357625.html