I’m using cURL to log in, like so:
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_URL,'http://www.website.com/login.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=example&password=example');
Then, once that’s run, I’m using this to authenticate further requests:
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
The trouble is, I can’t seem to make the code work unless I redo the login portion every time I run it; this seems impractical and I’m certain there’s a better way to go about it. Perhaps there’s a way to tell cURL how long to keep a cookie active?
Make sure you set
CURLOPT_COOKIEFILEandCURLOPT_COOKIEJARbefore you do any auth work.Also, make sure the cookie.txt is both readable and writable.