Zee M
Zee M

Reputation: 83

Handling multiple requests whilst using cURL's CURLOPT_COOKIEFILE option

I'm using CURLOPT_COOKIEJAR to store cookies to a file and CURLOPT_COOKIEFILE to retrieve them from the file.

What I'm wonder is what happens when multiple users are accessing the script at the same time - won't it mess up the contents of the cookie file? Also, how do I manage the cookie files so that it's possible to have multiple users at the same time?

Upvotes: 4

Views: 2476

Answers (3)

John Parker
John Parker

Reputation: 54445

You'll need to specify a different file for each execution of the script, otherwise you'll have issues with the file being overwritten, etc. as you suggest.

You might want to have a look at the tempnam (example below) as a means of generating the unique file, or simply use uniqid, etc. and create the file yourself.

<?php
    session_start();
    $cookieFilePath = $_SESSION['cookiefilepath']
                         ? $_SESSION['cookiefilepath']
                         : tempnam(sys_get_temp_dir(), session_id().'_cookie_');
    $_SESSION['cookiefilepath'] = $cookieFilePath;
    ...
    curl_setopt($curlSession, CURLOPT_COOKIEFILE, $cookieFilePath);
    ...
?>

That said, you'll need to ensure that you remove these files once they're no longer required. (If this isn't within the lifetime of your script, you might want to periodically execute a tidy-up script via cron that uses filemtime or similar.)

Incidentally, you can simply provide a full path to the file you want to use - it doesn't have to be in the same directory that the script is in, despite what is said in the existing Can someone explain CURL cookie handling (PHP)? question.

Upvotes: 3

Xavier Barbosa
Xavier Barbosa

Reputation: 3947

CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE are just utilities for handling cookies in a file, like a web browser. And it's not recommended for your case.

But you can play directly with http headers to set and retrieve cookies.

For setting you cookies

<?php
curl_setopt($ch, CURLOPT_COOKIE, 'user=xxxxxxxx-xxxxxxxx');
?>

For retrieving cookies, just identify the headers that startswith Set-Cookie:

You can check this document for understanding how cookie headers works http://curl.haxx.se/rfc/cookie_spec.html

Usage example, quick and dirty, but definitely not standard.

With this headers

<?php
$header_blob = '
Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo
';

Extract cookie headers

$cookies = array();
if (preg_match_all('/Set-Cookie:\s*(?P<cookies>.+?);/i', $header_blob, $matches)) {
    foreach ($matches['cookies'] as $cookie) {
        $cookies[] = $cookie;
    }
    $cookies = array_unique($cookies);
}
var_dump($cookies);

Resend cookies

$cookie_blob = implode('; ', $cookies);
var_dump($cookie_blob);

Upvotes: 3

Kris
Kris

Reputation: 41867

Multiple requests will overwrite the same file (but will probably also slow all other requests execution down due to file locking).

You could incorporate the session_id() into the cookie file name so you'll have one cookie file for every client session. I'd also recommend storing the files in something like sys_get_temp_dir().

something like:

$cookieFile = sys_get_temp_dir().PATH_SEPARATOR.session_id().'-cookies.txt';

Should work fine for that.

Upvotes: 1

Related Questions