Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL -- cookies and sessions

Tags:

php

curl

I would like to understand the four options of cURL for cookies:

CURLOPT_COOKIESESSION  
CURLOPT_COOKIEFILE
CURLOPT_COOKIEJAR
CURLOPT_COOKIE

I understand that COOKIEJAR is meant for writing cookies, and COOKIEFILE is meant for reading. So what is COOKIESESSION for ? The CURLOPT_COOKIE is custom, but can I use it to maintain a session with the server ?

like image 813
Ted Avatar asked Apr 23 '12 22:04

Ted


1 Answers

To understand CURLOPT_COOKIESESSION, you need to know a couple of things about cookies. Cookies have expiration dates that are set by the website that issues the cookie. If an expiration date of a cookie has passed, the browser/client will not send it, and it will be deleted by the client. If a cookie is set with NO expiration date, the browser should use that cookie until the browser session is closed, or the user logs out and the cookie gets unset.

That said, CURLOPT_COOKIESESSION is a way to get cURL to simulate having closed the browser. If the COOKIEFILE has some session cookies in it (cookies with no expiration), it will normally send these if they were present in the file. If you set CURLOPT_COOKIESESSION, then it will NOT send any of the cookies that have no expiration date.

CURLOPT_COOKIE just gives you a means of setting the cookie data that will be sent to the server in raw format. This is useful if for example you have a raw HTTP cookie that you would like to send. Without this option, you would have to get those cookies into the COOKIEFILE, or set a custom HTTP header Cookie: with the raw value you had.

like image 63
drew010 Avatar answered Sep 21 '22 17:09

drew010