Current Location: Home> Latest Articles> Use curl_share_init() to share cookies to improve login status reuse efficiency

Use curl_share_init() to share cookies to improve login status reuse efficiency

M66 2025-05-19

When writing PHP scripts for HTTP requests, cURL is a very important tool, especially when we need to simulate multiple requests from the browser, how to keep logged in or share a session becomes especially important. cURL provides a very useful function curl_share_init() , which can help us share cookies in multiple cURL sessions, avoiding login again every time a request is sent, thus greatly improving the reuse efficiency of login state.

1. Overview of curl_share_init() function

curl_share_init() is a function in PHP that creates a shared handle. Shared handles allow multiple cURL sessions to share the same resources, such as cookies, session data, etc. Using this function allows us to share the same cookie information in different HTTP requests, which is useful for a consistent login state between multiple requests.

In the following example, we will demonstrate how to use curl_share_init() to share cookies and improve the reuse efficiency of multiple requests.

2. Sample code

 <?php

// Create a shared handle
$share = curl_share_init();

// Configuration cURL Options,Using a shared handle
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "http://m66.net/login.php");  // Login pageURL
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_COOKIEJAR, "cookie.txt");  // keep Cookie
curl_setopt($ch1, CURLOPT_SHARE, $share);  // Share resources
$response1 = curl_exec($ch1);  // Perform the first request,Login operation
curl_close($ch1);

// Create a second cURL Session,Take advantage of previous shared handles
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "http://m66.net/dashboard.php");  // Pages that need to be logged in before accessing
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_SHARE, $share);  // Use the same shared handle
$response2 = curl_exec($ch2);  // Execute the second request,Get the user&#39;s personal information page
curl_close($ch2);

// Output the result of the second request
echo $response2;

// Close the shared handle
curl_share_close($share);
?>

3. How to work

  1. Create a shared handle :
    Use curl_share_init() to create a handle to a shared resource $share . This shared handle is used to share cookies and other data between different cURL sessions.

  2. The first request: Login :
    We use curl_init() to create the first cURL session $ch1 , setting the URL of the login page (assuming it is http://m66.net/login.php here). We also use CURLOPT_COOKIEJAR to specify a file ( cookie.txt ) to store cookies so that subsequent requests can continue to use these cookies.

  3. The second request: Obtain user information :
    Create a second cURL session $ch2 , setting the page URL that needs to be logged in (for example, http://m66.net/dashboard.php ). By setting CURLOPT_SHARE as the shared handle $share , we ensure that this request will use the cookies in the first request to reuse the login state.

  4. Close the shared handle :
    After the request is completed, we use curl_share_close() to close the shared handle $share and free the resource.

4. Advantages

  1. Improve efficiency : By sharing cookies, avoid logging in again every time you request, reducing unnecessary request overhead.

  2. Convenient to manage sessions : curl_share_init() allows multiple requests to share the same session data, making it convenient for long-standing session states.

  3. Simplified code : By sharing resources, the code becomes more concise and more convenient to maintain.

5. Things to note

  1. Resource management : Although shared handles provide a lot of convenience, it should be noted that the shared handle created by curl_share_init() will occupy a certain system resource. Therefore, after the use is completed, you must remember to call curl_share_close() to release the resources.

  2. Cookie path and domain name : Ensure that the storage path of cookie files is consistent with the domain name, and avoid the interference of cookie data between different domain names. If you send requests under different domain names, make sure to configure the cookie file appropriately.

  3. Concurrent requests : If you plan to make concurrent requests, curl_share_init() allows you to share cookie data, but you still need to be cautious when concurrent requests to prevent synchronization problems.