Alex Rivera | Logout

how to maintain session in cURL in php?

Asked 2010-03-11T12:06:21.723
22

how can we maintain session in cURL?

i'am having a code the sends login details of a site and logs in successfully i need to get the session maintained at the site to continue.

here is my code that used to login to the site using cURL

  <?php  
        $socket = curl_init();
        curl_setopt($socket, CURLOPT_URL, "http://www.XXXXXXX.com");
    curl_setopt($socket, CURLOPT_REFERER, "http://www.XXXXXXX.com");
    curl_setopt($socket, CURLOPT_POST, true);
    curl_setopt($socket, CURLOPT_USERAGENT, $agent);
    curl_setopt($socket, CURLOPT_POSTFIELDS, "form_logusername=XXXXX&form_logpassword=XXXXX");
    curl_setopt($socket, CURLOPT_COOKIESESSION, true);
    curl_setopt($socket, CURLOPT_COOKIEJAR, "cookies.txt");
    curl_setopt($socket, CURLOPT_COOKIEFILE, "cookies.txt");
    $data = curl_exec($socket);
    curl_close($socket); 
   ?>
Edit
Report

1 Answer

0

I would probably move that code into a function. After successful login, you now have the session associated with the cookie you have in your cookie.txt file. On subsequent requests, just keep using that cookie file and you should have a valid session on that site.

answered 2010-03-11T20:36:52.190

Your Answer