PHP 8.4: Curl: CURLOPT_DNS_USE_GLOBAL_CACHE no longer has any effect

Version8.4
TypeChange

The PHP Curl extension exposes a libcurl option that allowed Curl to use a shared global DNS cache. When enabled, the DNS information is cached between requests and Curl handles, even after a Curl handle is destroyed.

The option is enabled by setting the CURLOPT_DNS_USE_GLOBAL_CACHE Curl option via curl_setopt function. However, the libcurl Global DNS cache is not thread-safe, and in certain situations, it can lead to security vulnerabilities and undesired behavior. Thread-safe PHP builds prevent setting this option and emit a warning to alleviate this.

Libcurl deprecated this feature in version 7.11.1, and deprecated it in libcurl 7.62.0. In PHP 8.4 and later, regardless of the libcurl version, setting CURLOPT_DNS_USE_GLOBAL_CACHE no longer has any effect.

$ch = curl_init();
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, 1); // Has no effect

Recommended approach to cache DNS entries

By default, DNS entries are cached during the lifetime of a CurlHandle object. Reusing the same CurlHandle object automatically shares the DNS cache.

To share DNS cache between multiple requests, consider using CurlShareHandle objects created from the curl_share_init function.

When Curl relies on the system DNS resolver (for example, when not using DNS Over HTTPS), DNS entries will be cached by the system DNS resolver too — this is the ideal way to cache DNS entries because it provides standard mechanisms to flush DNS, and is compatible with other applications that can make use of a DNS cache too.

Backward Compatibility Impact

The CURLOPT_DNS_USE_GLOBAL_CACHE constant itself is not deprecated. However, PHP applications that set this Curl option can safely remove setting the CURLOPT_DNS_USE_GLOBAL_CACHE because it will not be functional in all PHP 8.4 builds and any PHP build with libcurl 7.62.0 or later.

In PHP 8.4, enabling CURLOPT_DNS_USE_GLOBAL_CACHE on thread-safe builds no longer triggers the thread-safety warning (CURLOPT_DNS_USE_GLOBAL_CACHE cannot be activated when thread safety is enabled).


CURLOPT_DNS_USE_GLOBAL_CACHE Implementation