PHP gethostbyname Efficiency issues

Source: Internet
Author: User
Tags nslookup
Recently used gethostbyname, in the local environment Mac+apache execution speed very fast, fraction seconds, but put on the server side is not, server-side environment Lnmp, execution time is more than 5 seconds, how to solve this problem?
Besides gethostbyname, is there any other way to resolve the domain name to IP?

Reply content:

Recently used gethostbyname, in the local environment Mac+apache execution speed very fast, fraction seconds, but put on the server side is not, server-side environment Lnmp, execution time is more than 5 seconds, how to solve this problem?
Besides gethostbyname, is there any other way to resolve the domain name to IP?

gethostbyname requires DNS resolution, which typically takes 1-5 seconds.
It is recommended to change to another method.

function Gethostbyname2 ($host, $timeout = 3) {   $query = ' nslookup-timeout= $timeout-retry=1 $host ';   if (Preg_match ('/\naddress: (. *) \n/', $query, $matches))      return Trim ($matches [1]);   return $host;}

Try your @april.l the code seems wrong. Modified a bit, and do not know how to do, gethostbyname the speed of execution suddenly came up, under 1s clock, I seem to have nothing changed, just slept a day. I don't know how.

function Gethostbyname2 ($host, $timeout = 3) {$query = ' nslookup-timeout= '. $timeout. '-retry=1 '. $host; $query = Shell_exe C ($query); if (Preg_match ('/\naddress: (. *) \n/', $query, $matches)) return trim ($matches [1]); return $host;}

PHP gethostbyname does not have any special logic, in most cases, it is directly called gethostbyname (3), and DNS parsing and your network environment is very relevant, so this is really not good generalize:)

The easy way is to install DNSMASQ on a machine in the local or LAN, and then refer to DNS in the past, as a DNS query acceleration, it will be much faster.

  • 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.