首页 > 编程技术 > php

用PHP实现标准的IPWhois查询

发布时间:2016-11-25 17:34

由于Internet的历史原因,apin负责整个网络IP的整体规划以及北美区 还有部分非洲地区的IP分配治理,与此相应的是,whois.apin.net是IP whois的root server,标准的IP whois查询方法是,首先向whois.apin.net查询某个IP属于哪个大区,然后再向该区的whois 服务器查询此IP的whois具体信息。

目前负责 IPV4的大区
whois.arin.net 美洲区 北美
whois.apnic.net 亚太区 包括亚洲和澳大利亚
whois.ripe.net 欧洲区 欧洲/中东(西亚)/北非
whois.lacnic.net 拉美区 拉丁美洲和加勒比海区域
非洲网络的IP查询也在whois.arin.net
向某个whois服务器提交whois查询的过程

打开一个到whois服务器的43端口的连接,然后发送要查询的域名 和一个回车换行。假如要查询多个域名,请用空格分开然后从sokect中读取结果。最后服务器将自动断开连接。

用PHP实现
1.验证IP(用 ip2long代替 ereg)
2.向 whois.arin.net查询,假如数据库中没有相关信息,会给出一个Referral Server的URL,格式如下
ReferralServer:
然后根据此信息,继续查询

代码如下

whoisip.php

<?php
include_once "lang.inc.php";

$IP = isset($_GET['ip'])?$_GET['ip']:'blank';
if (-1 === ip2long($ip))
die(str_replace('%IP%', $IP, $Text['ip_invalid']));

echo GetWhois($IP);

function GetWhois($IP)
{
global $Text;
$rootwhois = 'whois.arin.net';
$buffer = str_replace('%SERVER%', $rootwhois, $Text['sock_connect']);
$buffer1 = ReadSocket($rootwhois,$IP);
if ($buffer1 !== '')
{
$whois = SubStrByTag("ReferralServer: whois://"," ",$buffer1);
//remove port number ":43";
if ( ($pos=strpos($whois,":")) !== FALSE)
{
$whois = substr($whois, 0,$pos);
}
if ($whois !== '')
{
$buffer .= str_replace('%SERVER%', $whois, $Text['sock_connect']);
$buffer .= ReadSocket($whois,$IP);
}
else
{
$buffer .= $buffer1;
}
}
return nl2br($buffer);
}

function SubStrByTag($firstTag,$secondTag,&$longStr)
{
$firstPos = strpos ($longStr,$firstTag);
$ret = '';
if ($firstPos !== FALSE)
{
$secondPos = strpos ($longStr,$secondTag,$firstPos);
if ($secondPos !== FALSE)
{
$firstPos = strlen($firstTag);
$ret = substr($longStr,$firstPos,$secondPos-$firstPos);
}
}
return $ret;
}

function ReadSocket($whois,$ip)
{
global $Text;
$buffer = '';
if (!$sock = fsockopen( $whois, 43, $errNum, $errStr, 20))
{
$buffer = str_replace('%SERVER%', $whois, $Text['sock_fail']);
}
else
{
fputs($sock,"$ip ");
//$buffer = fread($sock, 8192);
while(!feof($sock)) $buffer.=fgets($sock, 8192);
fclose($sock);
}
return $buffer;
}

?>

语言文件:
lang.inc.php

<?php
$Text = Array(
'ip_invalid'=>'I want to get a valid IP, but it is [%IP%].',
'sock_connect'=>'Ask %SERVER% ...
',
'sock_fail'=>'Cannot connect to the host:%SERVER%'
);

?>

其他有名的whois服务器

1.Tucows (whois.opensrs.net) 一次只能一个连接
dnsstuff就是查询的它

2.BulkRegister (whois.bulkregiter.net) 小心它临时封IP,假如大量连接的话

3.Network Solutions (whois.networksolutions.com) 一天只能查1000次

4.Go Daddy (whois.godaddy.com)

5.whois.abuse.net

看到很多朋友在各个地方发帖问PHP生成静态文章系统的方法,以前曾做过这样一个系统,遂谈些看法,以供各位参考。好了,我们先回顾一些基本的概念。

  一,PHP脚本与动态页面。

  PHP脚本是一种服务器端脚本程序,可通过嵌入等方法与HTML文件混合,也可以类,函数封装等形式,以模板的方式对用户请求进行处理。无论以何种方式,它的基本原理是这样的。由客户端提出请求,请求某一页面 -----> WEB服务器引入指定相应脚本进行处理 -----> 脚本被载入服务器 -----> 由服务器指定的PHP解析器对脚本进行解析形成HTML语言形式 ----> 将解析后的HTML语句以包的方式传回给浏览器。由此不难看出,在页面发送到浏览器后,PHP就不存在了,已被转化解析为HTML语句。客户请求为一动态文件,事实上并没有真正的文件存在在那里,是PHP解析而成相对应的页面,然后发送回浏览器。这种页面处理方式被称为“动态页面”。

  二,静态页面。

  静态页面是指在服务器端确实存在的仅含HTML以及JS,CSS等客户端运行脚本的页面。它的处理方式是。由客户端提出请求,请求某一页面 ----> WEB服务器确认并载入某一页面 ----> WEB服务器将该页面以包的形式传递回浏览器。由这一过程,我们对比一下动态页面,即可方现。动态页面需由WEB服务器的PHP解析器进行解析,而且通常还需连接数据库,进行数据库存取操作,然后才能形成HTML语言信息包;而静态页面,无须解析,无须连接数据库,直接发送,可大大减轻服务器压力,提高服务器负载能力,大幅提供页面打开速度和网站整体打开速度。但其缺点是,不能动态地对请求进行处理,服务器上必须确实存在该文件。

  三,模板及模板解析。

  模板即尚未填充内容html文件。例如:

 temp.html

<HTML>
  <TITLE>{ title}</TITLE>
  <BODY>
   this is a { file} file's templets
  </BODY>
</HTML>

PHP处理:

 templetest.php

<?php
  $title = "网页教学网测试模板";
  $file  = "Webjx test templet,<br>author:web@";

 $fp= fopen ("temp.html","r");
  $content = fread ($fp,filesize ("temp.html"));
  $content .= str_replace ("{ file}",$file,$content);
  $content .= str_replace ("{ title}",$title,$content);

  echo $content;
?>



  模板解析处理,即将经PHP脚本解析处理后得出的结果填充(content)进模板的处理过程。通常借助于模板类。目前较流行的模板解析类有phplib,smarty,fastsmarty等等。模板解析处理的原理通常为替换。也有些程序员习惯将判定,循环等处理放进模板文件中,用解析类处理,典型应用为block概念,简单来说即为一个循环处理。由PHP脚本指定循环次数,如何循环代入等,再由模板解析类具体实施这些操作。

  好了,对比过静态页面与动态页面各自的优劣,现在我们就来说说,如何用PHP生成静态文件。

  PHP生成静态页面并不是指PHP的动态解析,输出HTML页面,而是指用PHP创建HTML页面。同时因为HTML的不可写性,我们创建的HTML若有修改,则需删掉重新生成即可。(当然你也可以选择用正则进行修改,但个人认为那样做倒不如删掉重新生成来得快捷,有些得不偿失。)

  言归正传。用过PHP文件操作函数的PHP FANS知道,PHP中有一个文件操作函数fopen,即打开文件。若文件不存在,则尝试创建。这即是PHP可以用来创建HTML文件的理论基础。只要用来存放HTML文件的文件夹有写权限(即权限定义0777),即可创建文件。(针对UNIX系统而言,Win系统无须考虑。)仍以上例为例,若我们修改最后一句,并指定在test目录下生成一个名为test.html的静态文件:

<?php
  $title = "网页教学网测试模板";
  $file  = "Webjx test templet,<br>author:web@";

 $fp     = fopen ("temp.html","r");
  $content = fread ($fp,filesize ("temp.html"));
  $content .= str_replace ("{ file}",$file,$content);
  $content .= str_replace ("{ title}",$title,$content);

  // echo $content;
 
  $filename = "test/test.html";
  $handle  = fopen ($filename,"w"); //打开文件指针,创建文件
  /*
 检查文件是否被创建且可写
  */
  if (!is_writable ($filename)){
   die ("文件:".$filename."不可写,请检查其属性后重试!");
  }
  if (!fwrite ($handle,$content)){  //将信息写入文件
   die ("生成文件".$filename."失败!");
  }
  fclose ($handle); //关闭指针
 
  die ("创建文件".$filename."成功!");
?>

  实际应用中常见问题解决方案参考:

  一,文章列表问题:

  在数据库中创建字段,记录文件名,每生成一个文件,将自动生成的文件名存入数据库,对于推荐文章,只需指向存放静态文件的指定文件夹中的该页面即可。利用PHP操作处理文章列表,存为字符串,生成页面时替换此字符串即可。如,在页面中放置文章列表的表格加入标记{ articletable},而在PHP处理文件中:

<?php
  $title = "网页教学网测试模板";
  $file  = "Webjx test templet,<br>author:web@";

 $fp     = fopen ("temp.html","r");
  $content = fread ($fp,filesize ("temp.html"));
  $content .= str_replace ("{ file}",$file,$content);
  $content .= str_replace ("{ title}",$title,$content);
 
  // 生成列表开始
  $list = '';
  $sql = "select id, title,filename from article";
  $query = mysql_query ($sql);
  while ($result = mysql_fetch_array($query)){
   $list .= '<a href='.$root.$result['filename'].' target=_blank>'.$result['title'].'</a><br>';

 

With the XML list of movies in hand, it's time to create a Flex application that extends the simplemovie.mxml player with the list of movies. This upgraded Flex application is shown in Listing 7.

Listing 7. mytube1.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="movieXmlData.send()">

<mx:HTTPService method="get" url="http://localhost:8080/movies.php" id="movieXmlData" result="onGetMovies( event )" />

<mx:Script>
import mx.rpc.events.ResultEvent;
import mx.controls.VideoDisplay;
import mx.controls.List;
import mx.rpc.http.HTTPService;
import mx.collections.ArrayCollection;

[Bindable]
private var movies : ArrayCollection = new ArrayCollection();

public function onGetMovies( event : ResultEvent ) : void
{
  var firstMovie : String = event.result.movies.movie[0].source.toString();
  videoPlayer.source = firstMovie;

  movies = event.result.movies.movie;
  movieList.selectedIndex = 0;
}

public function onPrevious() : void
{
  if ( movieList.selectedIndex == 0 )
    movieList.selectedIndex = movies.length - 1;
  else
    movieList.selectedIndex -= 1;
  videoPlayer.source = this.movieList.selectedItem.source.toString();
}

public function onPlay() : void
{
  videoPlayer.source = this.movieList.selectedItem.source.toString();
  videoPlayer.play();
}

public function onNext() : void
{
  if ( movieList.selectedIndex >= ( movies.length - 1 ) )
    movieList.selectedIndex = 0;
  else
    movieList.selectedIndex += 1;
  videoPlayer.source = this.movieList.selectedItem.source.toString();
}

public function onChange() : void
{
  videoPlayer.source = this.movieList.selectedItem.source.toString();
}
</mx:Script>

<mx:HBox width="100%" paddingLeft="10" paddingTop="10" paddingRight="10">
  <mx:VBox>
    <mx:VideoDisplay width="400" height="300" id="videoPlayer" complete="onNext()" />
    <mx:HBox width="100%" horizontalAlign="center">
       <mx:Button label="<<" click="onPrevious()" />
       <mx:Button label="Play" click="onPlay()" />
       <mx:Button label=">>" click="onNext()" />
    </mx:HBox>
    </mx:VBox>
    <mx:List width="100%" height="340" id="movieList"
      dataProvider="{movies}"
      change="onChange()"
      labelField="title"></mx:List>
</mx:HBox>

</mx:Application>

 

The first video starts right when I open the page. As I select each movie from the list on the right, the page reloads and shows the movie I selected.

How sweet and simple was that? One Flex file, one PHP file, and a little database magic for the backend, and viola! Video sharing!

The next step is to see whether you can enhance the user experience a bit by doing more of the work in Flex.

The Flex Interface, Part 1

If you want to provide a mechanism for Flex to show any movie, you must provide the Flex application with the list of movies. The most convenient way to do that is through XML. So, going back to PHP again, you need a page exports the movie list from the database as XML. This movies.php page is shown in Listing 6.

Listing 6. movies.php
<?php
require "DB.php";

$moviebase = 'http://localhost:8080/movies/';

header( 'content-type: text/xml' );

$dsn = 'mysql://root@localhost/movies';
$db =& DB::connect( $dsn );
if ( PEAR::isError( $db ) ) { die($db->getMessage()); }
?>
<movies>
<?php
$res = $db->query( 'SELECT title, source, thumb, width, height FROM movies' );
while( $row = $res->fetchrow( ) ) {
?>
  <movie title="<?php echo( $row[0] ) ?>" source="<?php echo( $moviebase.$row[1] ) ?>" 
   thumb="<?php echo( $moviebase.$row[2] ) ?>" width="<?php echo( $row[3] ) ?>"
   height="<?php echo( $row[4] ) ?>" />
<?php
}
?>
</movies>

 

Listing 4. simplemovie.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:VBox backgroundColor="white" width="400" height="335">
  <mx:VideoDisplay width="400" height="300" id="videoPlayer"
    source="{Application.application.parameters.movie}" />
  <mx:HBox width="100%" horizontalAlign="center">
    <mx:Button label="Play" click="videoPlayer.play()" />
  </mx:HBox>
</mx:VBox>
</mx:Application>
Listing 5. simptest.php
<?php
require "DB.php";

$moviebase = 'http://localhost:8080/movies/';

$dsn = 'mysql://root@localhost/movies';
$db =& DB::connect( $dsn );
if ( PEAR::isError( $db ) ) { die($db->getMessage()); }

$source = null;
$movieId = 1;
if ( array_key_exists( 'movie', $_GET ) )
  $movieId = $_GET['movie'];

$movies = array();
$res = $db->query( 'SELECT movieId, source, title FROM movies' );
while( $row = $res->fetchrow( ) ) {
  $movies []= $row;
  if ( $row[0] == $movieId )
    $source = $row[1];
}

if ( $source == null )
    $source = $movies[0][1];
?>
<html>
<body>
<table>
<tr><td valign="top">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="400" 
  height="335"
  codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
<param name="movie" value="simplemovie.swf" />
<param name="quality" value="high" />
<param name="flashVars" value="movie=<?php echo( $moviebase.$source ) ?>">
<embed" width=100% src="simplemovie.swf" quality="high"
  width="400" height="335" play="true"
  loop="false"
  quality="high"
  flashVars="movie=<?php echo( $moviebase.$source ) ?>"
  type="application/x-shockwave-flash"
  pluginspage="http://www.adobe.com/go/getflashplayer">
</embed>
</object>
</td><td valign="top">
<?php
foreach( $movies as $movie ) {
?>
<a href="simptest.php?movie=<?php echo( $movie[0] )?>"><?php echo( $movie[2] )?></a><br/>
<?php
}
?>
</td></tr></table>
</body>
</html>
 
标签:[!--infotagslink--]

您可能感兴趣的文章: