Cookie Jar class
This class should be used to handle cookies (storing cookies from HTTP response messages, and sending out cookies in HTTP request messages).
This class is mainly based on Cookies.pm <http://search.cpan.org/author/GAAS/libwww-perl-5.65/ lib/HTTP/Cookies.pm> from the libwww-perl collection <http://www.linpro.no/lwp/>. Unlike Cookies.pm, this class only supports the Netscape cookie spec <http://wp.netscape.com/newsref/std/cookie_spec.html>, not RFC 2965.
I've been looking at both the Netscape cookie spec and RFC 2965, a lot of the functions will be based on RFC 2965 simply because it covers details missed out by the netscape cookie spec.
Please consider this class in 'alpha' state at the moment, I've still got a lot of testing to do, and will need to compare cookie handling with some existing browsers. Any feedback appreciated.
Example:
1 $options = array(
2 'file_persistent' => 'cookies.txt',
3 'autosave' => true
4 );
5 $jar =& new Cookie_Jar($options);
6 $jar->add_cookie_header($my_request);
7 $jar->destroy();
See test_Cookie_Jar.php file for usage examples
CHANGES:
Located in Program_Root/Cookie_Jar.php (line 70)
Constructor
Will accept an associative array holding the cookie jar options
Add cookie header
Adds the relevant cookie header to the request message
request object
Clear cookies - [domain [,path [,name]]] - call method with no arguments to clear all cookies.
Clear session cookies
Clears cookies which have no expiry time set
Destroy
An opplication using a cookie jar must call this method when it has finished with the cookies.
Extract cookies - extracts cookies from the HTTP response message.
Get matching cookies
Only use this method if you cannot use add_cookie_header(), for example, if you want to use this cookie jar class without using the request class.
associative array containing 'domain', 'path', 'secure' keys
Get option value
option name
Load
Loads cookies from a netscape style cookies file.
location of the file, or leave blank to use options
Parse Set-Cookie values.
Only use this method if you cannot use extract_cookies(), for example, if you want to use this cookie jar class without using the response class.
array holding 1 or more "Set-Cookie" header values
associative array containing 'host', 'path' keys
Save persistent cookies
file to save to, leave out to use the "file_persistent" option value
Save session cookies
file to save to, leave out to use the "file_session" option value
Scan
Goes through all cookies passing the values through the callback function.
The callback function can be a method from another object (eg. array(&$my_obj, 'my_method')). The callback function should accept 3 arguments:
Example:
1 // $jar is our cookie jar with some cookies loaded
2 $name_to_delete = 'bla';
3 $jar->scan('delete_name', $name_to_delete);
4
5 // our callback function defined here
6 function delete_name(&$jar, $cookie_parts, $name_to_delete) {
7 if ($cookie_parts['name'] == $name_to_delete) {
8 $jar->clear($cookie_parts['domain'], $cookie_parts['path'], $cookie_parts['name']);
9 }
10 // must return true to tell scan() to continue with cookies
11 return true;
12 }
function name, or array holding an object and the method to call.
passed as the 3rd argument to $callback
Set Cookie
cookie name
cookie value
expiry time (null if session cookie, <= 0 will delete cookie)
Set option - set cookie jar options.
RECOGNISED OPTIONS:
- option name values(s) description ------------------------------------------------------------------------------ - file_persistent string persistent cookie file location - file_session string session cookie file location - autosave bool save cookies when destroy() is called
option name to set, or associative array to replace all existing options
option value, null to delete option
Documention generated on Wed, 16 Jul 2003 01:03:03 +0100 by phpDocumentor 1.2.0