0

I executed curl -I domainname in terminal and got following response :

HTTP/1.1 200 OK
Date: Tue, 21 Apr 2015 15:23:09 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Set-Cookie: PHPSESSID=8i9nold1uq077k5onvrhe8bml7; expires=Tue, 28-Apr-2015 15:23:09 GMT; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: max-age=3600
Pragma: cache
Content-Type: text/html; charset=UTF-8
X-Varnish: 426004
Age: 0
Via: 1.1 varnish-v4
Content-Length: 0
Connection: keep-alive

and

HTTP/1.1 200 OK
Date: Tue, 21 Apr 2015 15:23:06 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Set-Cookie: PHPSESSID=73lrlui1ek9hfbfeasv8kh6qh2; expires=Tue, 28-Apr-2015 15:23:06 GMT; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: max-age=3600
Pragma: cache
Content-Type: text/html; charset=UTF-8
X-Varnish: 426001
Age: 0
Via: 1.1 varnish-v4
Connection: keep-alive

In both response PHPSESSID value is different. What changes need to be done in code so that the value in PHPSESSID remains same for all hits for some limited time.

The site is developed in PHP codeigniter. Have to do this change so that varnish can work. As we see that varnish age = 0 for both hits. It fails to create a cache.

Although when the link is used in browser the sessionID remains same in browser cookie, till the site is browsed and/ or same page is called. Until the browser is closed. But varnish age appears 0 there as well.

Any help, Thank you!

Vivek
  • 11
  • 4
  • http://stackoverflow.com/questions/13020404/keeping-session-alive-with-curl-and-php –  Apr 21 '15 at 07:34

1 Answers1

1

You should use CURLOPT_COOKIEJAR to save the initial cookie, and then to send that original cookie on subsequent requests:

Example from http://docstore.mik.ua/orelly/webprog/pcook/ch11_04.htm :

$cookie_jar = tempnam('/tmp','cookie');

// log in  (your cookie is empy at this point)
$c = curl_init('https://bank.example.com/login.php?user=donald&password=b1gmoney$');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar);
$page = curl_exec($c); //cookie set after this request
curl_close($c);

// retrieve account balance (the cookie has been set by the login request)
$c = curl_init('http://bank.example.com/balance.php?account=checking');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar);
$page = curl_exec($c);
curl_close($c);

// make a deposit (your cookie is still set from the login request)
$c = curl_init('http://bank.example.com/deposit.php');
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, 'account=checking&amount=122.44');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar);
$page = curl_exec($c);
curl_close($c);

// remove the cookie jar
unlink($cookie_jar) or die("Can't unlink $cookie_jar");
  • Hey Rob, I tried the solution. This helped me keep the phpsessionid same for all curl, using the above code as you mentioned. Here is the header response. – Vivek Apr 21 '15 at 08:58
  • Are you sending it in the header too? If not, you need to actually specify you are sending that data in header. –  Apr 21 '15 at 09:27