Hello Stackoverflow community,
I have a really annoying issue with PHP CURL cookies that I have been trying to solve for months with no success.
I have two CURL functions that run subsequently to login a user to an external site and then get a page from that site, which contains useful data.
Here's the login function:
function login($url, $data) {
$login = curl_init();
curl_setopt($login, CURLOPT_COOKIEJAR, 'cookies_' . session_id() . '.txt');
curl_setopt($login, CURLOPT_TIMEOUT, 30000);
curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($login, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($login, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($login, CURLOPT_URL, $url);
curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($login, CURLOPT_POST, TRUE);
curl_setopt($login, CURLOPT_POSTFIELDS, $data);
$content = curl_exec ($login);
curl_close ($login);
unset($login);
unset($data);
return $content;
}
And the data fetching function which uses the cookie file generated by the login function to identify that the user has logged in:
function grab_data($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies_' . session_id() . '.txt');
curl_setopt($ch, CURLOPT_TIMEOUT, 30000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$content = curl_exec($ch);
curl_close($ch);
unset($ch);
unset($data);
return $content;
}
Now, onto the problem. The login function works fine, but the problem is that it has to save the cookies that external site generates to a file on the server. If a lot of users are logging that puts a lot of load on the server, is impractical and is a security hole because it saves personal user cookies to a file on the server, which is widely accessible. The cookies are very important because they contain session information and are required for the data fetching function to get the user data from the external site. So, I want to somehow store the personal user cookies internally, perhaps in a PHP variable but I have a hard time achieving that. I have already tried methods suggested in this question, which suggest setting CURLOPT_HEADER
to true
and then using preg_match
to extract the cookies from header but I had no luck because:
- If I disable
CURLOPT_COOKIEJAR
in the login function, the login fails. - If I set
CURLOPT_COOKIEJAR
to something like'-'
, login works, but then I have problem in the data fetching function because I don't know how to pass the extracted cookies to the data fetching function (I tried many methods and nothing worked except theCURLOPT_COOKIEFILE
variant.
Can anyone give me a working solution for storing cookies internally and the passing them to another request? If it is not possible to do that with CURL
, can you suggest another approach to acomplish this?
Thanks is advance. It is a really long post but I wanted to include as much information as I could, so you can better understand my needs.