Use PHP to implement standard IPWhois queries. Due to the historical reasons of the Internet, apin is responsible for the overall planning of the entire network IP address and IP allocation management in parts of Africa in North America. correspondingly, whois.apin.net is due to the historical reasons of the Internet, apin is responsible for the overall planning of the entire network IP address and IP address allocation management in parts of North America and Africa. correspondingly, whois.apin.net is the root server of IP whois, and the standard IP whois query method is, first, query whois.apin.net for the region of an IP address, and then query the whois details of this IP address from the whois server.
IPV4 region
Whois.arin.net America North America
Whois.apnic.net Asia Pacific includes Asia and Australia
Whois.ripe.net Europe/Middle East (Western Asia)/North Africa
Whois.lacnic.net latex and the Caribbean
IP address query for African networks is also in whois.arin.net.
The process of submitting a whois query to a whois server
Open a connection to Port 43 of the whois server, and then send the domain name to be queried and a line break with a carriage return. To query multiple domain names, separate them with spaces and read the results from sokect. The server will be automatically disconnected.
PHP implementation
1. verify the IP address (replace ereg with ip2long)
2. query whois.arin.net. if there is no relevant information in the database, a URL of the Referral Server is provided, in the following format:
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 ']);
$ Buffer1 = ReadSocket ($ rootwhois, $ IP );
If ($ buffer1! = '')
{
$ Whois = SubStrByTag ("ReferralServer: whois: //", "\ n", $ 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 \ n ");
// $ Buffer = fread ($ sock, 8192 );
While (! Feof ($ sock) $ buffer. = fgets ($ sock, 8192 );
Fclose ($ sock );
}
Return $ buffer;
}
?>
Language file:
Lang. inc. 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 %'
);
?>
Other famous whois servers
1. Tucows (whois.opensrs.net) can only be connected by one connection at a time
Dnsstuff is the query.
2. BulkRegister (whois.bulkregiter.net) is careful when it is temporarily blocked by an IP address. if there are many 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
There are also some IP address allocation management in Africa. correspondingly, whois.apin.net is IP whois...