0

So I use cURL to get data from page 1, fetch info from it and store it in an array. The next step is to load the next page, I do this by creating a new cURL with the same cookiejar and cookiefile. I see the page 1 correctly but page 2 displays: Session not found. Session may have expired. By looking at the header information I see that in page 1 it sets the cookie WCOOKIE=rd244o00000000000000000000ffff912c10f5o80.
In the headers of page 2 it sets the cookie WCOOKIE=rd244o00000000000000000000ffff912c10f6o80.
So this is why it thinks the session is expired, it doesn't load the cookie but the page tries to rewrite it. So my question: What must I do to let page 2 load the cookie set by page 1 instead of trying to set a new cookie?

Additional information: the loading of the two pages both happens in one file (1.php) because I need to store both the data in 1 array, and you need the cookie from page 1 to be able to request page 2.

Both pages are set to use headers:

$headers[] = "Accept: */*";  
$headers[] = "Connection: Keep-Alive";  
$headers[] = "Content-type: application/x-www-form-urlencoded;charset=UTF-8";  

Loading page 1:

$cSession = curl_init(); 
$tmpfname = dirname(__FILE__).'/cookie.txt';
curl_setopt($cSession, CURLOPT_COOKIEJAR, $tmpfname);
curl_setopt($cSession, CURLOPT_COOKIEFILE, $tmpfname);
curl_setopt($cSession, CURLOPT_HTTPHEADER,  $headers);
curl_setopt ($cSession, CURLOPT_COOKIESESSION, TRUE);

Loading page 2:

$ch = curl_init ();
$tmpfname = dirname(__FILE__).'/cookie.txt';
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfname);
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfname);
curl_setopt($ch, CURLOPT_HTTPHEADER,  $headers);
curl_setopt ($ch, CURLOPT_COOKIESESSION, TRUE);  

This ain't all the settings but I'm guessing I have to change something in these lines, if more info is needed please let me know and i'll post them.
Help much appreciated :)

vincent kleine
  • 724
  • 1
  • 6
  • 22

1 Answers1

1

Okay I finally I fixed it thanks to this post how to get the cookies from a php curl into a variable. I post it here for other people having the same problem.
First I turned on CURLOPT_HEADER in request 1. Then I used this code to get the cookie:

preg_match('/^Set-Cookie:\s*([^;]*)/mi', $result, $m);
parse_str($m[1], $cookies);
$cookie = $cookies['NAMEOFCOOKIEUNEEDHERE']; 

So then I could pass this cookie on in request 2 by using this:

curl_setopt($ch, CURLOPT_COOKIE, 'NAMEOFCOOKIEUNEEDHERE='.$cookie);

Hope this could help anyone

Community
  • 1
  • 1
vincent kleine
  • 724
  • 1
  • 6
  • 22