为php文件建立ip地址,IP地址查询网站的建立(基于qqwry数据库建立)

一、需要的工具:QQWry最新版(ip地址数据库)

二、数据库的导入

1、下载qqwry后导出txt格式的数据库,命名为ip.txt

2、在mysql数据库中建立名为ipdb的数据库

3、在ipdb数据库中建立表ip,sql语句如下

CREATE TABLE `ip` ( `id` int(11) unsigned NOT NULL auto_increment,`ip_begin` int(10) unsigned NOT NULL default '0', `ip_end` int(10) unsigned NOT NULL default '0',`address` varchar(255) NOT NULL default '',`place` varchar(255) default '',PRIMARY KEY (`id`))TYPE=MyISAM CHARACTER SET utf8 COLLATE utf8_general_ci;

4、运行数据库导入PHP脚本

将ip.txt文件和下边的脚本放同一目录下,然后运行脚本

header('Content-Type:text/html;charset=gb2312');

set_time_limit(100);//因为数据库很大,所以脚本运行时间设为100秒

$file1="ip.txt";//打开导出的数据库文本文件

$handle1=fopen($file1,'rb+');

$con=@mysql_connect("localhost","root","ssklzs") or die("数据库连接出错");

mysql_select_db("ipdb");

while($row=fgets($handle1))

{

$arr=explode(" ",$row);//处理文本问价

$cal=0;

$flag=0;

foreach($arr as $num=>$value)

{

if($value!=""&&$cal<3)

{

$information[]=$value;

unset($arr[$num]);

$cal++;

}

elseif($value!="")

{

$tmp[]=$value;

$flag=1;

}

elseif($cal>=3&&$flag==1)

{

$tmp[]=$value;

}

else{}

}

if($flag==1) $information[3]=implode(" ",$tmp);

unset($tmp);

///分解X.X.X.X的ip地址为整数格式

$ip_arr = explode(".",$information[0]);

$ipnum = 0;

//初始化

foreach($ip_arr as $i=>$s)

{ $ipnum += $s*pow(256,3-$i); }

$information[0]=$ipnum;

$ip_arr = explode(".",$information[1]);

$ipnum = 0;

//初始化

foreach($ip_arr as $i=>$s)

{ $ipnum += $s*pow(256,3-$i); }

$information[1]=$ipnum;

//

$information[3]=str_replace("'","/'",$information[3]);//处理地址中可能含有的'符号

$sql="insert into ip "//执行插入语句

."(ip_begin,ip_end,address,place) values ('$information[0]','$information[1]','$information[2]','$information[3]')";

mysql_query($sql);

unset($information);

}

fclose($handle1);

?>

这样整个ip数据库便导入到了mysql中

三、建立查询页面

index.php脚本如下:

IP地址查询 Made by Erebus

请输入要查询的ip地址

(ip数据库更新日期:2010年05月05日)

db_connect.php脚本如下:

$connect=mysql_connect("localhost","root","ssklzs") or die("数据库连接出错");

mysql_select_db("ipdb");

?>

show_result.php脚本如下:

IP查询结果

/*

* Created on 2010-6-10

*

* To change the template for this generated file go to

* Window - Preferences - PHPeclipse - PHP - Code Templates

*/

include("db_connect.php");

$ip=$_POST['search'];

//ip正则表达式

$pattern="//A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))/.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))/Z/";

if(preg_match($pattern,$ip)&&$ip!="")

{

$ip_arr = explode(".",$ip);

$ipnum = 0;

//初始化

foreach($ip_arr as $i=>$s)

{ $ipnum += $s*pow(256,3-$i); }

$sql="select * from ip where ip_begin <= $ipnum and ip_end >= $ipnum";

$result=mysql_query($sql);

while($row=mysql_fetch_array($result))

{

//print_r($row);

echo "

$row[3]

";

echo "

$row[4]

";

echo "

重新查询

";

}

}

else echo "输入的ip不合法,请重新输入";

?>

完毕!

欢迎访问!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值