首页 > 编程技术 > php

ip归属地查询代码

发布时间:2016-11-25 15:58

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>ip归属地</title>

</head>
<body>
<h1>IP地址查询</h1>
<form id="form1" name="form1" method="post" action="">
  <input type="text" name="ipinfo" id="ipinfo" style="border:solid 1px #fdbdd5" />
  <input type="submit" name="button" id="button" value="查询" style="border:solid 1px #fdbdd5;width:50px" />
</form>
<?php教程
include("ipcheck.class.php");
$ipinfo=$_POST['ipinfo'];
$iptest=new IpLocation();
echo $iptest->getlocation($ipinfo);
?>
</body>
</html>

//ipcheck.class.php代码如下

<?php

class IpLocation {
/**
* QQWry.Dat文件指针
* @var resource
*/
var $fp;
/**
* 第一条IP记录的偏移地址
*
* @var int
*/
var $firstip;
/**
* 最后一条IP记录的偏移地址
*
* @var int
*/

var $lastip;

/**
* IP记录的总条数(不包含版本信息记录)
*
* @var int
*/

var $totalip;

/**
* 返回读取的长整型数
*
* @access private
* @return int
*/

function getlong() {

//将读取的little-endian编码的4个字节转化为长整型数

$result = unpack('Vlong', fread($this->fp, 4));

return $result['long'];

}

/**
* 返回读取的3个字节的长整型数
*
* @access private
* @return int
*/

function getlong3() {

//将读取的little-endian编码的3个字节转化为长整型数

$result = unpack('Vlong', fread($this->fp, 3).chr(0));

return $result['long'];

}

/**
* 返回压缩后可进行比较的IP地址
*
* @access private
* @param string $ip
* @return string
*/

function packip($ip) {

// 将IP地址转化为长整型数,如果在PHP5中,IP地址错误,则返回False,

// 这时intval将Flase转化为整数-1,之后压缩成big-endian编码的字符串

return pack('N', intval(ip2long($ip)));

}

/**
* 返回读取的字符串
*
* @access private
* @param string $data
* @return string
*/

function getstring($data = "") {

$char = fread($this->fp, 1);

while (ord($char) > 0) { // 字符串按照C格式保存,以结束

$data .= $char; // 将读取的字符连接到给定字符串之后

$char = fread($this->fp, 1);

}

return $data;

}

/**
* 返回地区信息
*
* @access private
* @return string
*/

function getarea() {

$byte = fread($this->fp, 1); // 标志字节

switch (ord($byte)) {

case 0: // 没有区域信息

$area = "";

break;

case 1:

case 2: // 标志字节为1或2,表示区域信息被重定向

fseek($this->fp, $this->getlong3());

$area = $this->getstring();

break;

default: // 否则,表示区域信息没有被重定向

$area = $this->getstring($byte);

break;

}

return $area;

}

/**
* 根据所给 IP 地址或域名返回所在地区信息
*
* @access public
* @param string $ip
* @return array
*/

function getlocation($ip) {

if (!$this->fp) return null; // 如果数据文件没有被正确打开,则直接返回空

$location['ip'] = gethostbyname($ip); // 将输入的域名转化为IP地址
$ip = $this->packip($location['ip']); // 将输入的IP地址转化为可比较的IP地址
// 不合法的IP地址会被转化为255.255.255.255
// 对分搜索
$l = 0; // 搜索的下边界
$u = $this->totalip; // 搜索的上边界
$findip = $this->lastip; // 如果没有找到就返回最后一条IP记录(QQWry.Dat的版本信息)
while ($l <= $u) { // 当上边界小于下边界时,查找失败

$i = floor(($l + $u) / 2); // 计算近似中间记录

fseek($this->fp, $this->firstip + $i * 7);

$beginip = strrev(fread($this->fp, 4)); // 获取中间记录的开始IP地址

// strrev函数在这里的作用是将little-endian的压缩IP地址转化为big-endian的格式

// 以便用于比较,后面相同。

if ($ip < $beginip) { // 用户的IP小于中间记录的开始IP地址时

$u = $i - 1; // 将搜索的上边界修改为中间记录减一

}

else {

fseek($this->fp, $this->getlong3());

$endip = strrev(fread($this->fp, 4)); // 获取中间记录的结束IP地址

if ($ip > $endip) { // 用户的IP大于中间记录的结束IP地址时

$l = $i + 1; // 将搜索的下边界修改为中间记录加一

}

else { // 用户的IP在中间记录的IP范围内时

$findip = $this->firstip + $i * 7;
break; // 则表示找到结果,退出循环

}

}

}

//获取查找到的IP地理位置信息
fseek($this->fp, $findip);
$location['beginip'] = long2ip($this->getlong()); // 用户IP所在范围的开始地址
$offset = $this->getlong3();
fseek($this->fp, $offset);
$location['endip'] = long2ip($this->getlong()); // 用户IP所在范围的结束地址
$byte = fread($this->fp, 1); // 标志字节
switch (ord($byte)) {

case 1: // 标志字节为1,表示国家和区域信息都被同时重定向

$countryOffset = $this->getlong3(); // 重定向地址

fseek($this->fp, $countryOffset);

$byte = fread($this->fp, 1); // 标志字节

switch (ord($byte)) {

case 2: // 标志字节为2,表示国家信息又被重定向

fseek($this->fp, $this->getlong3());

$location['country'] = $this->getstring();

fseek($this->fp, $countryOffset + 4);

$location['area'] = $this->getarea();

break;

default: // 否则,表示国家信息没有被重定向

$location['country'] = $this->getstring($byte);

$location['area'] = $this->getarea();

break;
}

break;

case 2: // 标志字节为2,表示国家信息被重定向

fseek($this->fp, $this->getlong3());

$location['country'] = $this->getstring();

fseek($this->fp, $offset + 8);

$location['area'] = $this->getarea();

break;

default: // 否则,表示国家信息没有被重定向

$location['country'] = $this->getstring($byte);

$location['area'] = $this->getarea();

break;

}

if ($location['country'] == " CZ88.NET") { // CZ88.NET表示没有有效信息

$location['country'] = "未知";

}

if ($location['area'] == " CZ88.NET") {

$location['area'] = "";

}

//return $location;
echo "你的IP:".$location['ip']."<br />来自".$location['country'].$location['area'];

}

/**
* 构造函数,打开 QQWry.Dat 文件并初始化类中的信息
*
* @param string $filename
* @return IpLocation
*/

function IpLocation($filename = "QQWry.Dat") {

if (($this->fp = @fopen($filename, 'rb')) !== false) {

$this->firstip = $this->getlong();

$this->lastip = $this->getlong();

$this->totalip = ($this->lastip - $this->firstip) / 7;

//注册析构函数,使其在程序执行结束时执行

register_shutdown_function(array(&$this, '_IpLocation'));

}

}

/**
* 析构函数,用于在页面执行结束后自动关闭打开的文件。
*
*/

function _IpLocation() {

fclose($this->fp);

}

}

?>

<!-- 这是一款免费的手机号码归属地查询程序是php教程写的,包括查询实例与实例代码-->
<html>
<head>
<title>手机号码归属地查询</title>
</head>
<body>
<form action="index.php?action=search" method="POST">
<p>请输入你要查询的手机号码:<input type="text" name="phone"></p>
<p><input type="submit" value="查询手机号码归属地"></p></p>
</form>
<?php
if (isset($_GET["action"])){
if ("search"==$_GET["action"] ){
require ('function.php');
$phone=(isset($_POST["phone"]))?$_POST["phone"]:die ("请返回");
echo "你查询的手机号码<font color=red>".$phone."</font>属于<font color=red>".getphone($phone)."</font>";

}
?>
</body>
</html>

//function.php 文件
<?php

session_start();
   
function update($num,$info){
 $dbpath="xiaolin/";
 $len=strlen($num);
 if ( $len < 7 ){
 return "手机号码最低7位哦";
 }
 $par="[0-9]";
 for ($i=0;$i<$len;$i++){
  if(!ereg($par,substr($num,$i,1) ) ){
  return "手机号码只能为数字";
  }
 }
 $sunum=scandir($dbpath); //得到支持的手机号码前缀
  array_splice($sunum,0,1); //把当前目录取消
  array_splice($sunum,0,1); //把上一级目录去掉
 $sub=substr($num,0,3); //取得该号码的前三位
 if (in_array($sub,$sunum) ){
   $num1=ltrim(substr($num,3,4),"0");
  $search=file($dbpath.$sub);
  $tmp=$search[$num1];
  $search[$num1]=$num1.'='.$info." ";
  $fp1=fopen($dbpath.$sub.'1','wb+');
for ($i=0;$i<10000;$i++){
 //$phone=str_pad($i,4,"0",STR_PAD_LEFT);
 //$phoneinfo="";
 //$phoneinfo.=$phone."=";
 //$phoneinfo=(isset($search[$i]))?$search[$i]:"";
 //$phoneinfo.=" ";
 fwrite($fp1,$search[$i]);
}
fclose($fp1);
 echo "$num 已更新";
 }else{
 die ("暂不支持$sub");
 }
 }
function getphone($phone){
 $dbpath="xiaolin/";
 $len=strlen($phone);
 if ( $len < 7 ){
 return "手机号码最低7位哦";
 }
 $par="[0-9]";
 for ($i=0;$i<$len;$i++){
  if(!ereg($par,substr($phone,$i,1) ) ){
  return "手机号码只能为数字";
  }
 }
 $sunum=scandir($dbpath); //得到支持的手机号码前缀
  array_splice($sunum,0,1); //把当前目录取消
  array_splice($sunum,0,1); //把上一级目录去掉
 $sub=substr($phone,0,3); //取得该号码的前三位
 if (in_array($sub,$sunum) ){
   $num=ltrim(substr($phone,3,4),"0");
  $search=file($dbpath.$sub);
  $tmp=$search[$num];
  $result=substr($tmp,strpos($tmp,"=")+1,strlen($tmp)-strpos($tmp,"=")-2); //处理数据
  return (strlen($result)>1)?$result:"无数据";
 }else{
 return "暂不支持$sub";
 }
}

function check(){
 if (!isset($_SESSION["flag"]) ){
 die ("<p>请<a href='manage.php?action=login'>登录!</a></p>"); 
 }elseif ($_SESSION["flag"] != true){
 die ("<p>请<a href='manage.php?action=login'>登录!</a></p>"); 
 }
}

function getinfo(){
check();
 $nums=array("130","131","132","133","134","135","136","137","138","139","150","151","153","155","156","157","158","159");
 $counts="";
 for($j=0;$j<count($nums);$j++){
  $id=$j;
  if ($id >= count($nums) ){ die ("OVER"); }
   $nownum=$nums[$id]; //当前的号码段
  $dbpath="xiaolin/";
  $fp=fopen("xiaolin/$nownum",'r');
  while(!feof($fp)){
   $line=fgets($fp);
   $tmp=explode("=",$line);
   $num1[$tmp[0]]=substr($line,strpos($line,"=")+1,strlen($line)-strpos($line,"=")-2);
  }
  fclose($fp);
 $flag=0;
  for($i=0;$i<10000;$i++){
   $ser=str_pad($i,4,"0",STR_PAD_LEFT);
   if(!strlen($num1[$ser]) ==0 ){
   ++$flag;
  }
 }
 $counts+=$flag;
  echo "$nownum:段记录$flag</p>";
}
 echo "总计$counts";}
 function leftnav(){
  check();
?>
<div>
<div id="left">

</div>
<div id="right">

<?php
}
function showabout(){
 echo "<p>手机号码归属地查询</p>
";
 }
?>

$url ='http://data.alexa.com/data/?cli=10&dat=snba&ver=7.0&url='.$q;
  $xml = simplexml_load_file($url);
  //echo $xml;
  $SD=$xml->SD;
  //print_r($SD);
   $rank = number_format($SD[1]->POPULARITY['TEXT']);
   $rankdelta = number_format($SD[1]->RANK['DELTA']);
   
//$url = 'http://data.alexa.com/data/?cli=10&dat=snba&ver=7.0&url='.$q;
//  $xml = simplexml_load_file($url);
//  $SD=$xml->SD;
 //  $rank = number_format($SD->POPULARITY['TEXT']);
  // $rankdelta = number_format($SD->RANK['DELTA']);
   $reachrank = number_format($SD->REACH['RANK']);
   $title = $SD->TITLE['TEXT'];
   $linksin = $SD->LINKSIN['NUM'];
   $owner = $SD->OWNER['NAME'];
   $email = $SD->EMAIL['ADDR'];
   $phonenumber = $SD->PHONE['NUMBER'];
   $createddate = $SD->CREATED['DATE'];
   $speedtext = number_format($SD->SPEED['TEXT']);
   $speedpct = number_format($SD->SPEED['PCT']);
   $langlex = $SD->LANG['LEX'];
   $langcode = $SD->LANG['CODE'];
   $street = $SD->ADDR['COUNTRY'].' '.$SD->ADDR['STATE'].' '.$SD->ADDR['CITY'].' '.$SD->ADDR['STREET'].' '.$SD->ADDR['CITY'];
   $sitedesc = $xml->DMOZ->SITE['DESC'];
   $cat = $xml->DMOZ->SITE->CATS->CAT['ID'];?>

$k=trim($_GET['k']);
$u=trim($_GET['u']);
$arrKeywords=explode(",",$k);
for($ii=0;$ii<count($arrKeywords);$ii++){
$outputstr='';
  for($i=0;$i<10;$i++){
  $sourcecode= @file_get_contents("http://www.google.cn/search?num=100&q=".$arrKeywords[$ii]);
   if($sourcecode){
   break;
   }
  }
 preg_match_all('/<li class=g>(.*?)<cite>(.*?)//', $sourcecode, $urlmatches);
  for($j=0;$j<count($urlmatches[2]);$j++){
   if(strstr($urlmatches[2][$j],$u)){
   $outputstr.=1+$j.',';
   }
  }
   if($outputstr<>''){
    echo '<script type="text/javascript教程">parent.document.getElementById("Googlek'.$ii.'").innerHTML = "'.$outputstr.'";</script>';
   }else{
    echo '<script type="text/javascript">parent.document.getElementById("Googlek'.$ii.'").innerHTML = "排名100以外";</script>';
   }
}

<?php教程
header("Content-Type:text/html;charset=gbk");
include_once 'Textclass.php';
$url='111cn.net教程';
if(empty($url) || $url == '')$url = $_GET['message'];
if(preg_match("/(.*?)/$/i",$url)){
 $url=preg_replace("//$/","",$url);
}
$message=__urljudge(eregi_replace("http://","",$url));
$content=array(message=>$message,ip=>$Myip,time=>time());
$text_class->add_line($content);
function _link($url){
 $contents = @file_get_contents("$url");
 if($contents=="Forbidden" || $contents==""){
  $ch = curl_init();
  $timeout = 5;
  curl_setopt ($ch, CURLOPT_URL, "$url");
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $contents = curl_exec($ch);
  curl_close($ch);
 }
 if(empty($contents)){
  exit('<font color=red>cant locaion.</font>');
 }
 preg_match_all("/charset=(.*?)>/is",$contents,$cod);
 if(!empty($cod[1][0])){
  if(preg_match("/utf-8/i",$cod[1][0])){
   $contents=iconv("UTF-8","gbk//TRANSLIT",$contents);
  }
 }
 return $contents;
}
$contents=_link($url);
preg_match_all("/<a href=(.*?)</a>/is",$contents,$link);
foreach($link[0] as $val){
 if(strip_tags($val)){
  preg_match_all("/<a href="(.*?)"/is",$val,$link_url);
  $links[] = $val;
  if(preg_match("/http/i",$link_url[1][0])){
   if(!preg_match("/$message/i",$link_url[1][0])){
    $links_out[] = $link_url[1][0];
    $array[]= '<div class=list_left>'.strip_tags($val).'</div><div class=list_right><a href="'.$link_url[1][0].'">'.$link_url[1][0].'</a></div>';
   }else{
    $array[]= '<div class=list_left>'.strip_tags($val).'</div><div class=list_right><a href="'.$link_url[1][0].'">'.$link_url[1][0].'</a></div>';
   }
  }else{
   if(!preg_match("/^/(.*?)/",$link_url[1][0]))$link_url[1][0]='/'.$link_url[1][0];
   $array[]= '<div class=list_left>'.strip_tags($val).'</div><div class=list_right><a href="'.$url.$link_url[1][0].'">'.$url.$link_url[1][0].'</a></div>';
  }
 }
}

if(!empty($link)){
 echo "<b>网站:<font color=red>".$url."</font></b><br><br>";
 echo "<div id=list_top><b>共有链接<font color=red>".count($links)."</font>,内链<font color=red>".(count($links)-count($links_out))."</font>,外链<font color=red>".count($links_out)."</font></b><br><br></div>";
echo "<a href="./">返回查询首页</a>";
}else{echo "<br><br>网站:<font color=red>".$url."</font>无法查询,请更换查询地址!";}
?>
<form method="post" id="shoulu">
 <div class="pxd13">
网址:<input type="text" name="message" class="input_1" id="message">
   <input type="submit"   name="Submity" class="button"  value=" 提交 ">
 </div></form>
<?php
require_once 'Textclass.php';
$history=$text_class->openFile();
sort($history,SORT_DESC);
foreach($history as $k => $v){
 $h[] = $v[0];
}
if($h)$history = array_flip(array_flip($h));
?>
<div id="leftcontent_2"></div>
 <?php if ($history){
  foreach ($history as $val){
   echo "<a href=link.php?message=$val class='urls'>".$val."</a>";
  }
 }else{
  echo "<br><p>暂无记录</p>";
 }
 ?>
 </div>

标签:[!--infotagslink--]

您可能感兴趣的文章: