Login to page with curl in php and then visit link "logged in" Login to page with curl in php and then visit link "logged in" curl curl

Login to page with curl in php and then visit link "logged in"


You need to use the cookiejar parameter to share a text file between both requests.

/* Create a temporary cookie file */$ckfile = tempnam ("/tmp", "COOKIES");/* Visit the first page */$ch = curl_init ("http://somedomain.com/");curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);$output = curl_exec ($ch);/* Visit the second page */$ch = curl_init ("http://somedomain.com/cookiepage.php");curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);$output = curl_exec ($ch);

Example modified from http://coderscult.com/php-curl-cookies-example/


Edit: I should have read the docs for tempnam... It creates a unique filename, so adding an additional user specific prefix is not necessary unless you want to be able to identify the file later. You can also look at tmpfile() which is automatically removed when fclose is called (normally at the end of the php request).