Scale IPWhois query using PHP

Source: Internet
Author: User
Due to historical Internet reasons, apin is responsible for the overall plan of all network IP addresses and IP address allocation and governance in parts of Africa in North America. correspondingly, whois.apin.net is the rootserver of IPwhois, the scale IPwhois query method is: first, to wh due to historical Internet reasons, apin is responsible for the overall plan of all network IP addresses and IP address allocation and management in parts of Africa in North America. correspondingly, whois.apin.net is the root server of IP whois. the IP whois query method of the scale is to first query whois.apin.net which region an IP belongs, then, query the whois information of the 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. If you want to query multiple domain names, use spaces to exit and then read the results from sokect. The server will be 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 will be provided. the pattern is as follows:
ReferralServer:
Then, based on this information, the query continues.

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 );

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.