Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

curl_close() No Longer Flushes The Cookie Jar

Calling curl_close() on a handle that used a cookie jar would immediately write the collected cookies to disk, since the underlying resource was destroyed right away. Since PHP 8.0, cURL handles are objects, and curl_close() no longer destroys the handle: it only becomes a no-op that keeps the handle usable, so the cookie jar file is not written anymore until the object is actually garbage collected. Code relying on curl_close() to flush cookies to disk should instead call curl_setopt($handle, CURLOPT_COOKIELIST, "FLUSH") before closing the handle. Since PHP 8.5, calling curl_close() at all raises a deprecation notice, as core has confirmed the function has had no effect since 8.0.

PHP code

<?php

$jar = tempnam(sys_get_temp_dir(), 'cookiejar');

$ch = curl_init('https://httpbin.org/cookies/set?test=value');
curl_setopt($ch, CURLOPT_COOKIEFILE, '');
curl_setopt($ch, CURLOPT_COOKIEJAR, $jar);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);

var_dump(filesize($jar) > 0);

unlink($jar);

?>

Before

bool(true)

After

bool(false)

PHP version change

This behavior was deprecated in 8.5.

This behavior changed in 8.0.

See Also

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat