Handling multiple queries using the cURL parameter CURLOPT_COOKIEFILE

I use CURLOPT_COOKIEJAR to store cookies in a file and CURLOPT_COOKIEFILE to extract them from a file.

What I’m interested in is what happens when several users access the script at the same time - will this not ruin the contents of the cookie? Also, how do I manage cookies to have multiple users at the same time?

+4
source share
3 answers

You need to specify a different file for each script execution, otherwise you will have problems with a rewritable file, etc., as you suggest.

Perhaps you should take a look at tempnam (example below) as a means of creating a unique file, or just 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); ... ?> 

However, you will need to make sure that you delete these files as soon as they are no longer required. (If this is not during the life of your script, you may need to periodically run the tidy-up script through cron, which uses filemtime or similar.)

By the way, you can simply provide the full path to the file that you want to use - it should not be in the same directory as the script, despite what is said in the existing one. Can someone explain the question CURL cookie (PHP) ? .

+3
source

CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE are simply utilities for processing cookies in a file, for example, in a web browser. And this is not recommended for your case.

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

To set cookies

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

To receive cookies, simply specify headers that start with Set-Cookie:

You can check this document to understand how cookie headers work http://curl.haxx.se/rfc/cookie_spec.html

A use case, quick and dirty, but definitely not standard .

With these 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); 
+3
source

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

You can include session_id() in the cookie name so that you have one cookie for each client session. I also recommend storing files as sys_get_temp_dir() .

sort of:

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

Should work fine for this.

+1
source

Source: https://habr.com/ru/post/1337150/


All Articles