PHP & cURL proxy - how to make multi-user cookie jar?
Asked Answered
A

1

2

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

Albanian answered 20/4, 2011 at 14:18 Comment(0)
M
3

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).

Meistersinger answered 20/4, 2011 at 14:26 Comment(4)
Thanks for the reply. I think my question might be a bit ambiguous - when I say 'multi user' I mean 'multiple simultaneous users' rather than actually 'user managed'. What I take from your answer is that storing the remote session cookie in a jar defined by something unique to the current PHP session - eg session_id() is the right way to do things. Thanks for clearing that up for me.Albanian
You're welcome! But be warned that there is a CURL setting you must use to reset the cookies between each call, otherwise volatile cookies may leak between sessions. Look this up here for details: php.net/manual/en/function.curl-setopt.phpMeistersinger
Thank you for this information, does CURL create this cookie files automatically when you reference them? Or do they need to be created in the same script?Autostrada
@Pete - Yes, CURL should create the files automatically.Meistersinger

© 2022 - 2024 — McMap. All rights reserved.