PHP GetHostByName() Slow? Make it Faster

Posted on February 14, 2019 at 10:52 pm

Sometimes gethostbyname() can take up to 5 seconds and more to complete.

Here are two smart tricks to make it faster:

// Set timeout and retries to 1 to have a max execution time of 1 second for the DNS lookup
putenv('RES_OPTIONS=retrans:1 retry:1 timeout:1 attempts:1');
 
// Make sure the domain ends in a dot to prevent DNS recursion lookups
$ip = @gethostbyname("google.com.");

Thansk to Dennis for these awesome tips:

http://php.net/manual/en/function.gethostbyname.php#118841

It works also with gethostbyaddr().

Updated on April 16, 2019 at 11:36 pm

Receive updates via email

Other Posts

Updated Posts