Hey guys,

I'm having a helluv a time trying to get cURL working with the cookiejar. The idea is that I should be able to make a request to the login page to start the session, then use cURL to hit other pages to collect informatino.

I can login with the first request, this works fine. I then check the cookie file I've set and the data is in there. When I make the second connection it has lost the session. The headers indicate that it is using a different session id, and the cookie file does not get updated.

I'm at a loss on how I can get this thing to have a persistent connection, any help would be much appreciated.

Here is a sloppy version of what I'm working with.

$ch = curl_init();
$cookie_file_path = 'my_cookie.txt';
$cookie_file_path = realpath($cookie_file_path);

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);

curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

//new ones
curl_setopt($ch, CURLOPT_HEADER, 1);

curl_setopt($ch, CURLOPT_URL,"https://myurl/login.php?login=user&password=password");

echo curl_exec ($ch); // execute the curl command
echo 'Curl error: ' . curl_error($ch); //no errrors
curl_close ($ch);
unset($ch);

$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);

curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_URL,"https://myurl/report.php?option1=value1&option2=value2");

//new ones
curl_setopt($ch, CURLOPT_HEADER, 1);

echo curl_exec ($ch);
echo 'Curl error: ' . curl_error($ch);

curl_close ($ch);

I've tried it as both get and post. Adding the header opt is new and appears to have made no difference other then allowing me to confirm my session id is changing. Am I even going about this the right way?

    cookie text, as I'm not sure it's relevant.

    # Netscape HTTP Cookie File
    # http://curl.haxx.se/rfc/cookie_spec.html
    # This file was generated by libcurl! Edit at your own risk.
    my_url.com	FALSE	/	FALSE	0	PHPSESSID	259xxx7d
    
    

      Foolish. I wasn't submitting the the submit input name properly, so the script I was trying to login to never actually logged in, but gave me a screen like it did. Being that I was never actually logged in I had no session that could persist. Now that the login works correctly I do not have issues.

        On second review, it wasn't just the login variable that was messing things up.

        I'm having to run each request with the same curl handler to keep the session persistent. If I close it out and recreate it then the second request will fail. Using the following good I get what I want, but as I understand this will give me issues if I need to post data. Right now all requests are GET, so the post data doesn't build up.

        $ch = curl_init();
        $cookie_file_path = 'my_cookie.txt';
        $cookie_file_path = realpath($cookie_file_path);
        
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
        
        curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        
        //new ones
        curl_setopt($ch, CURLOPT_HEADER, 1);
        
        curl_setopt($ch, CURLOPT_URL,"https://myurl/login.php?login=user&password=password");
        
        echo curl_exec ($ch); // execute the curl command
        //echo 'Curl error: ' . curl_error($ch); //no errrors
        //curl_close ($ch);
        //unset($ch);
        
        //$ch = curl_init();
        //curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
        //curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
        
        //curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
        //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
        //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        
        curl_setopt($ch, CURLOPT_URL,"https://myurl/report.php?option1=value1&option2=value2");
        
        //new ones
        curl_setopt($ch, CURLOPT_HEADER, 1);
        
        echo curl_exec ($ch);
        echo 'Curl error: ' . curl_error($ch);
        
        curl_close ($ch); 
        

          I'm unsure as to why any of the examples for using cURL do not include this, but to make any of the login examples and my own code work, I had to add this option for the opening connection:

          curl_setopt($ch, CURLOPT_COOKIESESSION, true);
          

          I then create the new ch as normal where it previously failed but now it works. So I've acheived the same result of my fix, the issue I had with my fix is if I had to make another post request, i would have the left over vars from the first request. These are now properly cleared.

            Write a Reply...