Home  >  Article  >  Backend Development  >  php 根据数据库ip段显示ip归属地

php 根据数据库ip段显示ip归属地

WBOY
WBOYOriginal
2016-06-13 12:26:061014browse

php 依据数据库ip段显示ip归属地
我现在有一个数据库ipatt 三个字段 ip_start(起始IP)   ip_end(结束IP)   ip_att(IP归属地)
现在有$user_ip 
我要实现ip在某个ip_start   ip_end区间时 显示ip_att

假如表中包含这两条
0.0.0.0         0.255.255.255                  A
12.0.64.0    12.194.255.255                B
我希望实现
 0.11.12.12               显示A
 12.193.65.200        显示B

求教如何比较IP
我现在要写个 function get_ip_att($user_ip )
------解决思路----------------------
MySQL 提供有 INET_ATON 函数,用于将字符串型的 ip 转换为无符号数
php 也提供有 ip2long 函数完成此工作

$ip = ip2long('12.193.65.200');
$sql = "select * from ipatt where $ip between inet_aton(ip_start) and inet_aton(ip_end)";

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn