$cookiefile = '/tmp/curl-session'; $loginUrl = 'http://locahost/test'; $user = urlencode($username); $pass = urlencode($password); $ch = curl_init(); // set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, $loginUrl); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$user&pass=$pass"); curl_setopt($ch, CURLOPT_TIMEOUT, 30); // grab URL and pass it to the browser $output = curl_exec($ch);Now any subsequent request to the same domain URL using the same cookie file will maintain the session until timed out. Happy PHPing.
Nice one there. This saved me much time.
Thumbs up!
Well done Eric.
Thanks for pointing that out.
Just a litte advice: is good to use curl_close($ch); in order to close the curl session.
Hi thanks for your post, how do I make a subsequent request using the same cookie file?
Many thanks for you help
Hi Vincenzo,
Thanks for visiting my blog. I believe you just need to specify the cookie location the same as the one used by the login, it should work:
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
Can you please give it a try?
Cheers