tags:

views:

34

answers:

1

I would like to login to a site, so the first time I request a page, it redirects me to another page setting the cookies.

I am following a tutorial where they specify doing this

$cookie = '/tmp/cookies.txt';
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);

But when i check http live headers, the server passes cookie information to set my cookies. But i don't see it doing anything. When I examine the cookies, those values aren't there.

So do I have to specify another path for $cookie?

A: 

You've to use CURLOPT_COOKIEFILE for sending cookies instead of CURLOPT_COOKIE.

From the docs for function curl_setopt():

CURLOPT_COOKIE

The contents of the "Cookie: " header to be used in the HTTP request. Note that multiple cookies are separated with a semicolon followed by a space (e.g., "fruit=apple; colour=red")

CURLOPT_COOKIEFILE

The name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file.

CURLOPT_COOKIEJAR

The name of a file to save all internal cookies to when the handle is closed, e.g. after a call to curl_close.

Lekensteyn
let's say the the server returns this: Set-Cookie: mail_USERNAME=deleted; expires=Wed, 14-Oct-2009 20:38:46 GMT; path=/; domain=.mail.com\r\n Doesn't my cookiefile path have to be: /mail.com to match the header information
stone
The 'file' path and 'cookie' path are totally different things. The 'file' path is the location on your filesystem, where the Cookie jar can be found. The 'cookie' path is part of the cookie, hinting the client (in this case cURL) to send those cookies for a specific path only. So if you'd requested `http://example.com/dir/file`, `path` could be `/` or `/dir/`.
Lekensteyn