Php: how to obtain the IP address of a domain name function-PHP source code

Source: Internet
Author: User
Ec (2); & nbsp; the Code is as follows: copy the code $ dmwww.111cn.net; $ ipgethostbyname ($ dm); echogethostbyaddr ($ ip); echo $ ip; the two outputs are the same, * gethostbyname syntax stringgethostbyname (stri script ec (2); script

The Code is as follows:
$ Dm = 'www .111cn.net ';
$ Ip = gethostbyname ($ dm );
Echo gethostbyaddr ($ ip );
Echo $ ip;

// The two outputs are the same,
/*
Gethostbyname syntax

String gethostbyname (string $ hostname)

Returns the ipv4 address of the Internet host specified by the host name.
Returns the ipv4 address of the internet host specified by hostname


The following is an example. This is the best method I have proposed to solve the problem of any host name to an IP address. It is fast, reliable, and has timed out! An invalid address, such as a unicode string, returns the last 4? Seconds instead of 8 calls gethostbyname ?! It can only work with unix though.
*/

The Code is as follows:
Function getaddrbyhost ($ host, $ timeout = 3 ){
$ Query = 'nslookup-timeout = $ timeout-retry = 1 $ host ';
If (preg_match ('/address: (. *)/', $ query, $ matches ))
Return trim ($ matches [1]);
Return $ host;
}

/*
Gethostbyaddr is used to obtain a specific IP address corresponding to the internet host name.

The Code is as follows:
String gethostbyaddr (string $ ip_address)

*/

The Code is as follows:

$ Hostname = gethostbyaddr ($ _ server ['remote _ addr ']);

Echo $ hostname;

This site original tutorial reprinted www.111cn.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.