PHP & cURL proxy - how to make multi-user cookie j

2019-07-12 08:45发布

问题:

I'm developing an application that does a remote login, amongst other things, via cURL.

The remote site gives out a session cookie, which I can store in my cookie jar.

I want each user to my site to have a unique session on the remote site. My application works fine with just one user (me), but I'm not sure how to make it multiuser.

My first thought is to set a session variable for my application users, then use this variable as the name of the cookie jar, but this seems ugly.

Is there any built-in PHP/cURL functionality that will pass the unique session from the remote server to my users?

Many thanks for any help.

Jack

回答1:

Your question has every element of a solution, namely cookie jar and sessions.

When you provide the cookie jar file to CURL simply give it a name according to your user, example:

$protected_cookie_dir='/cookies/';
$uid=getUser()->id; // get the user id
curl_set_opt($ch,CURLOPT_COOKIEFILE,$protected_cookie_dir.'file_'.$uid.'.data');
curl_set_opt($ch,CURLOPT_COOKIEJAR,$protected_cookie_dir.'jar_'.$uid.'.data');

Important: Be sure to hide that folder (maybe store it outside your document root).