Using PHP to implement standard IP whois query _php Foundation

Source: Internet
Author: User
Tags go daddy port number

Due to the historical reasons for the Internet, Apin is responsible for the overall network IP planning as well as the North American region also has some of Africa's IP allocation management, and accordingly, whois.apin.net is the IP whois root server, the standard IP whois query method is , first query the whois.apin.net for which area an IP belongs to, and then query the WHOIS server for that IP for whois details.

Currently responsible for the IPV4 of the large area
Whois.arin.net Americas North America
Whois.apnic.net Asia and Australia
Whois.ripe.net Europe/Middle East (West Asia)/North Africa
Latin America and Caribbean region of the Latin American region of Whois.lacnic.net
IP queries for African networks 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 to be queried and a carriage return line wrap. If you want to query multiple domain names, separate them with spaces and read the results from the sokect. The last server will automatically disconnect.

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

The code is as follows

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 ']);
$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);
}
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 \ n");
$buffer = Fread ($sock, 8192);
while (!feof ($sock)) $buffer. =fgets ($sock, 8192);
Fclose ($sock);
}
return $buffer;
}

?>

Language files:
lang.inc.php

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

?>

Other known WHOIS servers

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

2.BulkRegister (whois.bulkregiter.net) take care of it temporarily sealed IP, if a large number of connections

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

4.Go Daddy (whois.godaddy.com)

5.whois.abuse.net

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.