Current Location: Home> Latest Articles> How to share authentication status and login sessions between requests

How to share authentication status and login sessions between requests

M66 2025-05-24

curl_share_init() is a function provided by the PHP cURL extension to initialize a cURL session shared object. With this object, we can share session data between different cURL requests. Especially when making requests that require authentication, this shared session data can significantly reduce duplicate login operations and improve the efficiency of the program.

 $ch_share = curl_share_init();

The above code initializes a shared resource object $ch_share , which you can then associate with multiple cURL sessions to share some information.

2. Use curl_share_setopt() to set sharing options

Once you have created a shared object, you can set what information needs to be shared through curl_share_setopt() . Common shared data include:

  • CURL_SHARE_COOKIE : Shared Cookies

  • CURL_SHARE_HEADER : Share header information

  • CURL_SHARE_REQUEST : Share request information

 curl_share_setopt($ch_share, CURL_SHARE_COOKIE, true);

With the above settings, multiple requests can share cookie data, which is essential for maintaining login status.

3. Share the session in multiple cURL requests

Once the shared object is initialized and the relevant options are set, we can use this shared resource in different cURL requests. First, create multiple cURL sessions and bind the shared resource object to each session.

 $ch1 = curl_init();
$ch2 = curl_init();

// Bind a shared resource to cURL Session
curl_setopt($ch1, CURLOPT_SHARE, $ch_share);
curl_setopt($ch2, CURLOPT_SHARE, $ch_share);

// Set requested URL(Replace it with the actual request)
curl_setopt($ch1, CURLOPT_URL, 'https://m66.net/api/login');
curl_setopt($ch2, CURLOPT_URL, 'https://m66.net/api/data');

// Execute a request
curl_exec($ch1);
curl_exec($ch2);

// 关闭Session
curl_close($ch1);
curl_close($ch2);

In the above example, $ch1 and $ch2 share authentication status and cookie information, so they can both use the same login session.

4. Release shared resources

When all cURL sessions are completed, remember to free up shared resources to avoid memory leaks:

 curl_share_close($ch_share);

5. Example of usage: Shared authentication status

Suppose we need to access multiple API endpoints after logging in and stay logged in. We can share authentication status and cookie information through curl_share_init() and curl_share_setopt() . Here is a complete example:

 <?php
// Initialize shared resources
$ch_share = curl_share_init();

// Set sharing options(shared Cookie)
curl_share_setopt($ch_share, CURL_SHARE_COOKIE, true);

// Create the first one cURL Session(Login Request)
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_SHARE, $ch_share);
curl_setopt($ch1, CURLOPT_URL, 'https://m66.net/api/login');
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_POST, true);
curl_setopt($ch1, CURLOPT_POSTFIELDS, [
    'username' => 'your_username',
    'password' => 'your_password'
]);

// 执行Login Request
$response1 = curl_exec($ch1);

// Create a second cURL Session(Get data)
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_SHARE, $ch_share);
curl_setopt($ch2, CURLOPT_URL, 'https://m66.net/api/userdata');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);

// Perform data requests
$response2 = curl_exec($ch2);

// Output result
echo $response1;
echo $response2;

// 关闭Session
curl_close($ch1);
curl_close($ch2);

// 释放shared资源
curl_share_close($ch_share);
?>

In this example, the login request and the data request share the authentication status and cookies, so duplicate login operations can be avoided.

Summarize

With curl_share_init() and curl_share_setopt() , we are able to share authentication status and session information between multiple cURL requests, significantly improving efficiency when multiple requests. In scenarios where complex API calls or when you need to keep logged in, this method can help developers reduce unnecessary duplicate authentication operations and save time and resources.