Class Cookie_Jar

Description

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:

    • Modified add_cookie_header() to call the push_header() method of the HTTP Request class.
    • Modified extract_cookies() to call the get_header_array() method of the HTTP Response class.
      • Initial release
      TODO:
      • testing

      Located in Program_Root/Cookie_Jar.php (line 70)

      
      	
      			
      Method Summary
      void Cookie_Jar ([array $options = null])
      void add_cookie_header (object &$request)
      void clear ([string $domain = null], [string $path = null], [string $name = null])
      void destroy ()
      void extract_cookies (object &$response)
      string get_matching_cookies (array $param)
      string get_option (string $option)
      bool load ([string $file = null])
      void parse_set_cookies (array $set_cookies, array $param)
      bool save ()
      bool save_persistent_cookies ([string $file = null])
      bool save_session_cookies ([string $file = null])
      void scan (mixed $callback, mixed &$param)
      void set_cookie (string $domain, string $path, string $name, string $value, [bool $secure = false], [int $expires = null])
      void set_option (mixed $option, [string $value = null])
      Methods
      Constructor Cookie_Jar (line 99)

      Constructor

      Will accept an associative array holding the cookie jar options

      void Cookie_Jar ([array $options = null])
      • array $options
      add_cookie_header (line 114)

      Add cookie header

      Adds the relevant cookie header to the request message

      void add_cookie_header (object &$request)
      • object $request:

        request object

      clear (line 377)

      Clear cookies - [domain [,path [,name]]] - call method with no arguments to clear all cookies.

      void clear ([string $domain = null], [string $path = null], [string $name = null])
      • string $domain
      • string $path
      • string $name
      clear_session_cookies (line 395)

      Clear session cookies

      Clears cookies which have no expiry time set

      void clear_session_cookies ()
      destroy (line 531)

      Destroy

      An opplication using a cookie jar must call this method when it has finished with the cookies.

      void destroy ()
      extract_cookies (line 192)

      Extract cookies - extracts cookies from the HTTP response message.

      void extract_cookies (object &$response)
      • object $response
      get_matching_cookies (line 138)

      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.

      string get_matching_cookies (array $param)
      • array $param:

        associative array containing 'domain', 'path', 'secure' keys

      get_option (line 340)

      Get option value

      • return:

        false if option not found

      string get_option (string $option)
      • string $option:

        option name

      load (line 477)

      Load

      Loads cookies from a netscape style cookies file.

      bool load ([string $file = null])
      • string $file:

        location of the file, or leave blank to use options

      parse_set_cookies (line 215)

      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.

      void parse_set_cookies (array $set_cookies, array $param)
      • array $set_cookies:

        array holding 1 or more "Set-Cookie" header values

      • array $param:

        associative array containing 'host', 'path' keys

      save (line 494)

      Save cookies using files specified in the options.

      bool save ()
      save_persistent_cookies (line 519)

      Save persistent cookies

      bool save_persistent_cookies ([string $file = null])
      • string $file:

        file to save to, leave out to use the "file_persistent" option value

      save_session_cookies (line 508)

      Save session cookies

      bool save_session_cookies ([string $file = null])
      • string $file:

        file to save to, leave out to use the "file_session" option value

      scan (line 440)

      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:

      1. A reference to the cookie jar object (&$jar)
      2. An array holding all cookie parts, array is associative with the following keys: ('domain','path','name','value','expires','secure')
      3. An optional parameter which can be used for whatever your function wants :), even though you might not have a use for this parameter, you need to define your function to accept it. (Note: you can have this parameter be passed by reference)
      The callback function should return a boolean, a value of 'true' will tell scan() you want it to continue with the rest of the cookies, 'false' will tell scan() to not send any more cookies to your callback function.

      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 }

      void scan (mixed $callback, mixed &$param)
      • mixed $callback:

        function name, or array holding an object and the method to call.

      • mixed $param:

        passed as the 3rd argument to $callback

      set_cookie (line 355)

      Set Cookie

      void set_cookie (string $domain, string $path, string $name, string $value, [bool $secure = false], [int $expires = null])
      • string $domain
      • string $path
      • string $name:

        cookie name

      • string $value:

        cookie value

      • bool $secure
      • int $expires:

        expiry time (null if session cookie, <= 0 will delete cookie)

      set_option (line 321)

      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
       

      void set_option (mixed $option, [string $value = null])
      • mixed $option:

        option name to set, or associative array to replace all existing options

      • string $value:

        option value, null to delete option

      Documention generated on Wed, 16 Jul 2003 01:03:03 +0100 by phpDocumentor 1.2.0