Use PHP to implement standard IPWhois queries

Source: Internet
Author: User
& Nbsp; due to historical Internet reasons, apin is responsible for the overall planning of the entire network IP address and IP address allocation management in some regions in North America, correspondingly, whois.apin.net is the rootserver of IPwhois. The standard IPwhois query method is to first go to whois. apin.

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.

Using php (as the mainstream development language)
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 (as the mainstream development language)

Include_once "lang. inc. php (as the mainstream development language )";

$ 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: //", "", $ 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 ))
{

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.