I've adapted the following code from developer.zend.com
Code: Select all
$url = "http://www.mydomain.co.uk/sphider/admin/auth.php";
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // times out after 4s
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=".urlencode('user)."&pass=".urlencode('password)."&submit=".urlencode('Log in')); // add POST fields
$result = curl_exec($ch); // run the whole process
print_r(curl_getinfo($ch));
echo "\n\ncURL error number:" .curl_errno($ch);
echo "\n\ncURL error:" . curl_error($ch);
curl_close($ch);CURLOPT_FOLLOWLOCATION is set to true, so as far as I understand it should have followed the header redirect that followed the successful username and password check, no?
Second question, assuming the login script worked and I'm logged in, I then need to visit a second url which is on the admin menu. Do I set the new url after the first curl_exec and then run curl_exec again before the curl session is closed with curl_close, or do I run a new curl routine starting from curl_init? My guess is that the second method would kill the session vars that were initialized by the first curl session.