Php how to obtain domain name IP address code function

Source: Internet
Author: User
How does php obtain the domain name IP address? the code function first looks at the following code. The two outputs are the same:

$ Dm = 'www .phpfensi.com ';

$ Ip = gethostbyname ($ dm );

Echo gethostbyaddr ($ ip );

Echo $ ip;

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 example shows the best method. I have proposed to solve the problem of any host name to IP address. it is fast, reliable, and has timed out. it is an invalid address, such as a unicode string, 4? Seconds instead of 8 calls gethostbyname? It can only work with unix, although 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 an IP address corresponding to the internet host name. The code is as follows:

String gethostbyaddr (string $ ip_address)

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

Echo $ hostname;

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.