Using PHP to implement standard Ipwhois query _php tutorials

Source: Internet
Author: User
Due to the historical reasons of the Internet, Apin is responsible for the overall planning of the entire network IP, as well as the North American region also has some African region IP allocation governance, accordingly, whois.apin.net is the IP whois root server, the standard IP whois query method is , first query the whois.apin.net to the region where an IP belongs, and then query the WHOIS server for that IP for whois specific information.

The region currently responsible for IPV4
Whois.arin.net Americas North America
Whois.apnic.net Asia Pacific, including Australia
Whois.ripe.net Europe/Middle East (West Asia)/North Africa
Whois.lacnic.net Latin America and Caribbean area
IP queries for the African network are also whois.arin.net
The process of submitting a WHOIS query to a WHOIS server

Open a 43 port connection to the WHOIS server, and then send the domain name you want to query and a carriage return line. If you want to query multiple domain names, separate them with a space and read the results from the sokect. The last server will automatically disconnect.

Implemented in PHP
1. Verify IP (replace Ereg with Ip2long)
2. To whois.arin.net query, if there is no relevant information in the database, will give a referral server URL, the format is as follows
Referralserver:
Then, based on this information, continue to query

The code is as follows

whoisip.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 ']);
$buffer 1 = readsocket ($rootwhois, $IP);
if ($buffer 1!== ")
{
$whois = Substrbytag ("referralserver:whois://", "", $buffer 1);
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. = $buffer 1;
}
}
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, $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;
}

?>

Language files:
lang.inc.php

$Text = Array (
' Ip_invalid ' = ' I want to get a valid IP, but it's [%ip%]. ',
' Sock_connect ' = ' Ask%SERVER% ...
',
' Sock_fail ' = ' cannot connect to the host:%server% '
);

?>

Other well-known WHOIS servers

1.Tucows (whois.opensrs.net) Only one connection at a time
Dnsstuff is the one that queries it.

2.BulkRegister (whois.bulkregiter.net) Beware of it temporarily sealing IP, if a large number of connections

3.Network Solutions (whois.networksolutions.com) can only be checked 1000 times a day.

4.Go Daddy (whois.godaddy.com)

5.whois.abuse.net

http://www.bkjia.com/PHPjc/632015.html www.bkjia.com true http://www.bkjia.com/PHPjc/632015.html techarticle due to the historical reasons of the Internet, Apin is responsible for the overall planning of the entire network IP, as well as the North American region also has some African region IP allocation governance, and accordingly, whois.apin.net is the IP whois ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.