黑帽联盟

标题: 用php查询域名的注册时间和注册状态 [打印本页]

作者: yun    时间: 2017-2-26 22:43
标题: 用php查询域名的注册时间和注册状态
用php查询域名的注册时间和注册状态,想看下面效果图,直接上代码:

效果演示:
whios查询
  1. <?php
  2. class SearchDomain
  3. {
  4. var $domain="";
  5. function SetDomain($udomain)
  6. {
  7. $this->domain = $udomain;
  8. }
  9. //
  10. // 获取whois并分析域名状态
  11. // ok 未被注册
  12. // 非空值 过期时间
  13. // 空值 未知
  14. //
  15. function GetInfo()
  16. {
  17. /*
  18. $dinfo = trim($this->GetWhois());
  19. if($dinfo=="") return "";
  20. if(eregi("no match",$dinfo)) return "ok";
  21. //return $rs;
  22. */
  23. $wl = "";
  24. $w_server = $this->GetServer();
  25. if($w_server=="") return "";
  26. $fp = fsockopen($w_server, 43, $errno, $errstr, 30);
  27. if(!$fp)
  28. {
  29. echo $errstr;
  30. return "";
  31. }
  32. $out = $this->domain."\r\n";
  33. $out .= "Connection: Close\r\n\r\n";
  34. fputs($fp, $out);
  35. while (!feof($fp))
  36. {
  37. $wl = fgets($fp, 255);
  38. if(eregi("no match",$wl))
  39. {
  40. fclose($fp);
  41. return "ok";
  42. }
  43. if(eregi("Expiration Date",$wl))
  44. {
  45. $lines = split(":",$wl);
  46. $t = trim($lines[1]);
  47. $ts = split(" ",$t);
  48. $t = $ts[0];
  49. if(ereg("[^0-9-]",$t))
  50. {
  51. $ts = split("-",$t);
  52. $t = $ts[2]."-".$this->MonthToNum($ts[1])."-".$ts[0];
  53. }
  54. fclose($fp);
  55. return $t;
  56. }
  57. }
  58. fclose($fp);
  59. return "";
  60. }
  61. //
  62. //获得域名的整个whois信息
  63. //
  64. function GetWhois()
  65. {
  66. $wh = "";
  67. $w_server = $this->GetServer();
  68. if($w_server=="") return "";
  69. $fp = fsockopen($w_server, 43, $errno, $errstr, 30);
  70. if(!$fp)
  71. {
  72. echo $errstr;
  73. return "";
  74. }
  75. $out = $this->domain."\r\n";
  76. $out .= "Connection: Close\r\n\r\n";
  77. fputs($fp, $out);
  78. while (!feof($fp))
  79. {
  80. $wh .= nl2br(fgets($fp, 255));
  81. }
  82. fclose($fp);
  83. return $wh;
  84. }
  85. //
  86. //输出当前域名的状态信息
  87. //
  88. function PrintSta()
  89. {
  90. $rs = $this->GetInfo();
  91. if($rs=="ok") echo $this->domain." 未注册!
  92. \r\n";
  93. else if($rs=="") echo "无法查询 ".$this->domain." 状态!
  94. \r\n";
  95. else echo $this->domain." 已注册,到期时间:$rs
  96. \r\n";
  97. }
  98. //
  99. //获得 whois 查询服务器
  100. //
  101. function GetServer()
  102. {
  103. $udomain=substr($this->domain,-3);
  104. switch($udomain)
  105. {
  106. case "com":
  107. $w_server="whois.internic.net";
  108. break;
  109. case "net":
  110. $w_server="whois.internic.net";
  111. break;
  112. case "org":
  113. $w_server="whois.pir.org";
  114. break;
  115. case "nfo":
  116. $w_server="whois.afilias.info";
  117. break;
  118. case "biz":
  119. $w_server="whois.biz";
  120. break;
  121. case ".cc":
  122. $w_server="whois.nic.cc";
  123. break;
  124. case "edu":
  125. $w_server="whois.educause.net";
  126. break;
  127. case "gov":
  128. $w_server="whois.nic.gov";
  129. break;
  130. case ".cn":
  131. $w_server="whois.cnnic.net.cn";
  132. break;
  133. default:
  134. $w_server="";
  135. }
  136. return $w_server;
  137. }
  138. //
  139. //英语的月份转为数字
  140. //
  141. function MonthToNum($m)
  142. {
  143. $m = strtolower($m);
  144. for($i=1;$i<=12;$i++)
  145. {
  146. $tt = mktime(0,0,0,$i+1,0,2005);
  147. if($m==strtolower(strftime("%b",$tt)))
  148. {
  149. if($i>9) return $i;
  150. else return "0".$i;
  151. }
  152. }
  153. }
  154. }

  155. $sd = new SearchDomain();
  156. $sd->SetDomain("cnblackhat.com");

  157. //查询域名是否被注册,等价于 $sd->PrintSta();
  158. $rs = $sd->GetInfo();
  159. if($rs=="ok") echo $sd->domain." 未注册!
  160. \r\n";
  161. else if($rs=="") echo "无法查询 ".$sd->domain." 状态!
  162. \r\n";
  163. else echo $sd->domain." 已注册,到期时间:$rs
  164. \r\n";

  165. //获得域名的详细whois信息
  166. //echo $sd->GetWhois();
  167. ?>
复制代码





欢迎光临 黑帽联盟 (https://bbs.cnblackhat.com/) Powered by Discuz! X2.5