Current Location: Home> Latest Articles> Why does my shared cookies not work?

Why does my shared cookies not work?

M66 2025-05-22

When PHP uses cURL to perform multiple HTTP requests, sometimes we want to share certain data between multiple requests, such as cookies, DNS caches, or SSL sessions. At this time, you can use curl_share_init() and related functions to achieve sharing. However, many developers encounter a common problem when trying to use the feature: This article will analyze the causes and solutions of this problem in detail.

1. Review of basic usage

First, let's quickly review how to use curl_share_init to share cookies:

 $sh = curl_share_init();
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);

$ch1 = curl_init('https://m66.net/login');
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_COOKIEFILE, '');
curl_setopt($ch1, CURLOPT_SHARE, $sh);
// Assume that this request will be set Cookie
$response1 = curl_exec($ch1);

$ch2 = curl_init('https://m66.net/profile');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_COOKIEFILE, '');
curl_setopt($ch2, CURLOPT_SHARE, $sh);
// I hope this will send the last requested settings here Cookie
$response2 = curl_exec($ch2);

curl_close($ch1);
curl_close($ch2);
curl_share_close($sh);

From the logic of the code, we expect to realize the sharing of cookies through CURL_LOCK_DATA_COOKIE of curl_share_setopt() . But many developers have reported that the second request does not come with the cookie returned by the first request .

2. Why are cookies not shared?

1. CURLOPT_COOKIEJAR or CURLOPT_COOKIEFILE is not set as a valid path

Although we set shared cookies, libcurl will not automatically save or write cookies by default unless set:

  • CURLOPT_COOKIEFILE : Read existing cookie files

  • CURLOPT_COOKIEJAR : Write cookie file after request

Set to '' (empty string) can enable in-memory cookie processing, but this method may be unstable when combined with curl_share_* .

2. Implementation difference between libcurl and PHP binding

The cURL extension of PHP uses the libcurl library. On some platforms or versions, curl_share_*' s behavior may differ slightly from the official documentation description, especially the portions involving cookies. CURL_LOCK_DATA_COOKIE is more designed for use by low-level libcurl , while the call method after PHP is encapsulated inconsistently supports it.

3. Cookie path or domain name mismatch

Cookies have path (Path) and domain (Domain) restrictions. If the requested URL domain name is different (even if the subdomain name is different), the cookie will not be sent. Similarly, if the cookie set by the server is only valid for a specific path and the next requested path is not included, the cookie will not be automatically attached.

3. Recommended solutions

Method 1: Use CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR

Using temporary cookie files for explicit management is the easiest and most reliable way.

 $tmpCookieFile = tempnam(sys_get_temp_dir(), 'cookie');

$ch1 = curl_init('https://m66.net/login');
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_COOKIEJAR, $tmpCookieFile);
curl_setopt($ch1, CURLOPT_COOKIEFILE, $tmpCookieFile);
$response1 = curl_exec($ch1);

$ch2 = curl_init('https://m66.net/profile');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_COOKIEJAR, $tmpCookieFile);
curl_setopt($ch2, CURLOPT_COOKIEFILE, $tmpCookieFile);
$response2 = curl_exec($ch2);

This method does not rely on curl_share_* and ensures that cookies can be stored and reloaded, and is suitable for most PHP projects.

Method 2: Manually manage cookie headers

If you need to control cookies more carefully, such as selecting only specific key values, you can capture the response header through CURLOPT_HEADERFUNCTION and then manually set it through CURLOPT_COOKIE :

 $cookies = [];

$ch1 = curl_init('https://m66.net/login');
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$cookies) {
    if (preg_match('/^Set-Cookie:\s*(.*?)=([^;]*)/i', $header, $matches)) {
        $cookies[$matches[1]] = $matches[2];
    }
    return strlen($header);
});
curl_exec($ch1);
curl_close($ch1);

$cookieString = '';
foreach ($cookies as $k => $v) {
    $cookieString .= "$k=$v; ";
}

$ch2 = curl_init('https://m66.net/profile');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_COOKIE, $cookieString);
$response2 = curl_exec($ch2);

4. Conclusion

curl_share_init() provides a mechanism to share data such as cookies, but be especially careful when using it in PHP. The sharing behavior of cookies is affected by a variety of factors, including domain names, paths, PHP encapsulation, and versions of libcurl themselves. If you want to share cookies between multiple requests, the safest way is to explicitly manage cookies using file methods , or manually extract and set cookies instead of relying on curl_share_init .

By understanding its underlying principles, we can more effectively build stable HTTP client logic, avoiding the trap of "it seems to be shared but actually not taking effect".