php cURL cookies/authentication not sticking? [closed]
I am learning cURL in PHP, and need to authenticate to a web page, and store the session in a cookie. I am able to log in just fine, but when I try to use the cookie to load another page, I don't get any output. It's late, and I'm sure there's something stupid I'm missing, but I dont think I'll be able to sleep without this working..
$curl_p = curl_init($auth_url);
curl_setopt($curl_p, CURLOPT_COOKIEJAR, $svck);
curl_setopt($curl_p, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_p, CURLOPT_RETURNTRANSFER, 1);
$cres_auth = curl_exec($curl_p);
if(!preg_match("/Successful/i",$cres_auth))
{
?><pre><?
print_r("Login was unsuccessful. URL passed was:");
?><br><br><?
print_r($auth_url);
?></pre><?
}else
{
?><pre><?
print_r("Login was successful.");
?></pre><?
$curl_p = curl_init($data_url);
curl_setopt($curl_p, CURLOPT_COOKIEFILE, $svck);
curl_setopt($curl_p, CURLOPT_URL, $data_url);
$cres_data = curl_exec($curl_p);
echo $cres_data;
}
Given the proper credentials, I get a "Login was successful", otherwise it fails. If successful, I don't get anything else; do i need to do curl_close() and re-init? Thanks!
edit
Not sure if it matters, but here is the header for the response when logging in normally (through a browser):
POST /admin HTTP/1.1
Host: url.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Referer: http开发者_StackOverflow社区s://url.com/admin?cmd=submit_logout
Cookie: svanoncollected=collected; password=4cf0ed4321adabbf46784e123a0316fb; extension=4470; iar=8123123f2b3e3541456cae084aeae77f;
lang_locale=en_us
Content-Type: application/x-www-form-urlencoded
Content-Length: 96
HTTP/1.1 200 OK
Date: Wed, 29 Jun 2011 07:43:11 GMT
Server: Apache/2.2.3 (Fedora)
Set-Cookie: lang_locale=en_us; path=/
Set-Cookie: admin_pass=74927c9b91234d2ffb95e60fc832ffe0; path=/
Set-Cookie: admin_name=user; path=/
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
edit
I was logging into https://.. and did not include curl_setopt($curl_p, CURLOPT_SSL_VERIFYPEER, false); in the if().. block. Adding it appears to have worked. Thanks again everyone!
精彩评论