Using PHP to implement standard IP whois query _php tutorial

Source: Internet
Author: User
Tags go daddy
Due to the historical reasons of the Internet, Apin is responsible for the overall planning of the entire network IP, as well as the IP allocation management of some African regions in North America, and accordingly, is the root server of IP whois, the standard IP whois query method is , first query the for the region to which an IP belongs, and then query the WHOIS server for that IP for whois details.

The region currently responsible for IPV4 Americas North America Asia Pacific, including Australia Europe/Middle East (West Asia)/North Africa Latin America and Caribbean area
IP queries for the African network are also
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 query, if there is no relevant information in the database, will give a referral server URL, the format is as follows
Then, based on this information, continue to query

The code is as follows


Include_once "";

$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 = ' ';
$buffer = Str_replace ('%SERVER% ', $rootwhois, $Text [' sock_connect ']);
$buffer 1 = readsocket ($rootwhois, $IP);
if ($buffer 1!== ")
$whois = Substrbytag ("referralserver:whois://", "\ n", $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);
$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 ']);
Fputs ($sock, "$IP \ n");
$buffer = Fread ($sock, 8192);
while (!feof ($sock)) $buffer. =fgets ($sock, 8192);
Fclose ($sock);
return $buffer;


Language files:

$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 ( Only one connection at a time
Dnsstuff is the one that queries it.

2.BulkRegister ( Watch it temporarily seal the IP if a large number of connections are

3.Network Solutions ( can only be checked 1000 times a day.

4.Go Daddy ( true techarticle due to the historical reasons of the Internet, Apin is responsible for the overall planning of the whole network IP and the IP allocation management in North America and some African regions, and accordingly, is the IP whois ...

  • Related Article

    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: 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.