Reputation: 139
I have multiple application nodes under a load-balancer which is making it problematic for me to save cookies on file as it might save on one node but then next request might point to the other node where the file doesn't exist.
This is the code I use to do the request which works fine on a single node but not on multiple ones:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
if ($this->proxy) {
curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
}
if (!is_null($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->settingsPath . $this->username . '-cookies.dat');
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->settingsPath . $this->username . '-cookies.dat');
if ($post) {
curl_setopt($ch, CURLOPT_POST, count($post));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
}
$resp = curl_exec($ch);
So is there any other way I could grab the "Set-Cookie" headers directly, save them into a session and re-use them instead of using COOKIEJAR and COOKIEFILE? I know there is CURLOPT_COOKIE, however my response header seems to have multiple "Set-Cookie" references and I don't know how to format them to use in CURLOPT_COOKIE.
Or at least is there a way to store the cookie files with CURLOPT_COOKIEJAR in a centralized storage server and read them back from there and how would I go on doing so?
Thanks in advance.
Upvotes: 0
Views: 376
Reputation: 21473
store/load the cookies from a database. you can use the browser client's cookie session id as the db id (aka what you get from session_id()). also, don't hardcode cookie locations, that's asking for trouble (what happens when 2 people load the same page with the same hardcoded cookie location at the same time? or what happens if you're on a read-only filesystem, where PHP doesn't have write-access?), just use tmpfile.
Upvotes: 1