initial commit
[m6w6/m6w6.github.io] / _posts / 2006-05-22-cookie-handling.md
1 ---
2 title: Cookie Handling
3 author: m6w6
4 tags:
5 - PHP
6 ---
7
8 I noticed some _weirdance_ about how libcurl and thus pecl/http handles
9 cookies.
10
11 I had to implement some changes which are only in CVS for now and which I'm
12 going to outline here:
13
14 ```php
15 $r = new HttpRequest("http://www.google.at/");
16 $r->recordHistory = true;
17 // we don't care about cookies by default
18 // enable automatic recognition of cookies
19 $r->enableCookies();
20 $r->send();
21 // received cookies will be sent on the next request
22 $r->send();
23 // reset those "auto" cookies
24 // this needs at least libcurl >= v7.14.1
25 $r->resetCookies();
26 $r->send();
27 echo $r->getHistory()->toString(true);
28 ```
29
30 Beware that all this does not affect custom cookies set with
31 [HttpRequest::setCookies](http://dev.iworks.at/ext-http/http-functions.html.gz#HttpRequest_setCookies)() and
32 [HttpRequest::addCookies](http://dev.iworks.at/ext-http/http-functions.html.gz#HttpRequest_addCookies)().
33 Custom cookies can always be unset by calling HttpRequest::setCookies().
34
35 A final note on using the **cookiestore** option:
36
37 ```php
38 $r = new HttpRequest("http://www.google.at/");
39 // load and save cookies from /tmp/cookies.txt
40 // if 'cookiesession' is TRUE, session cookies
41 // won't be loaded from the cookiestore
42 $r->setOptions(array(
43 "cookiesession" => TRUE,
44 "cookiestore" => "/tmp/cookies.txt")
45 );
46 $r->send();
47 ```
48
49 Note that using the cookiestore automatically enables libcurls cookie engine.
50