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文件和下边的脚本放同一目录下,然后运行脚本

<?php 
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脚本如下:

<html>
<head>
<meta http-equiv="Content-Language" content="en" />
<meta name="GENERATOR" content="PHPEclipse 1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>IP地址查询 Made by Erebus</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#FF9966" vlink="#FF9966" alink="#FFCC99">
<p>请输入要查询的ip地址</p>
<p>(ip数据库更新日期:2010年05月05日)</p>
<form name="ip_search" action="show_result.php" method="post">
<input type="text" name="search" value="" size="30" maxlength="30"/>
<input type="submit" name="submit" value="查询"/>
</form>
</body>
</html>

db_connect.php脚本如下:

<?php
$connect=mysql_connect("localhost","root","ssklzs") or die("数据库连接出错");
mysql_select_db("ipdb");
?>

show_result.php脚本如下:

<html>
<head>
<meta http-equiv="Content-Language" content="en" />
<meta name="GENERATOR" content="PHPEclipse 1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>IP查询结果</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#FF9966" vlink="#FF9966" alink="#FFCC99">
<?php
/*
* 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 "<p>$row[3]</p>";
echo "<p>$row[4]</p>";
echo "<p><a href="index.php" mce_href="index.php">重新查询</a></p>";
}
}
else echo "输入的ip不合法,请<a href="index.php" mce_href="index.php">重新输入</a>";
?>
</body>
</html>

完毕!

演示:http://erebus.coolpage.biz/ip

欢迎访问!!

posted @ 2011-08-27 22:46  Erebus_NET  阅读(476)  评论(0编辑  收藏  举报