Use PHP to implement standard IP Whois Query

Source: Internet
Author: User
Tags go daddy

Due to historical Internet reasons, apin is responsible for the overall planning of the entire network IP address and IP allocation management in some African regions in North America. Correspondingly, whois.apin.net is the root server of IP whois, the standard IP whois query method is to first query whois.apin.net which region an IP belongs to, and then query the whois details of this IP address from the whois server in the region.

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

<? 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

<? 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

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.