3 * Cookie storage object
10 * Cookie storage object
15 class Requests_Cookie {
30 * Valid keys are (currently) path, domain, expires, max-age, secure and
35 public $attributes = array();
38 * Create a new cookie object
41 * @param string $value
42 * @param array $attributes Associative array of attribute data
44 public function __construct($name, $value, $attributes = array()) {
46 $this->value = $value;
47 $this->attributes = $attributes;
51 * Format a cookie for a Cookie header
53 * This is used when sending cookies to a server.
55 * @return string Cookie formatted for Cookie header
57 public function formatForHeader() {
58 return sprintf('%s=%s', $this->name, $this->value);
62 * Format a cookie for a Set-Cookie header
64 * This is used when sending cookies to clients. This isn't really
65 * applicable to client-side usage, but might be handy for debugging.
67 * @return string Cookie formatted for Set-Cookie header
69 public function formatForSetCookie() {
70 $header_value = $this->formatForHeader();
71 if (!empty($this->attributes)) {
73 foreach ($this->attributes as $key => $value) {
74 // Ignore non-associative attributes
75 if (is_numeric($key)) {
79 $parts[] = sprintf('%s=%s', $key, $value);
83 $header_value .= '; ' . implode('; ', $parts);
89 * Get the cookie value
91 * Attributes and other data can be accessed via methods.
93 public function __toString() {
98 * Parse a cookie string into a cookie object
100 * Based on Mozilla's parsing code in Firefox and related projects, which
101 * is an intentional deviation from RFC 2109 and RFC 2616. RFC 6265
102 * specifies some of this handling, but not in a thorough manner.
104 * @param string Cookie header value (from a Set-Cookie header)
105 * @return Requests_Cookie Parsed cookie object
107 public static function parse($string, $name = '') {
108 $parts = explode(';', $string);
109 $kvparts = array_shift($parts);
114 elseif (strpos($kvparts, '=') === false) {
115 // Some sites might only have a value without the equals separator.
116 // Deviate from RFC 6265 and pretend it was actually a blank name
119 // https://bugzilla.mozilla.org/show_bug.cgi?id=169091
124 list($name, $value) = explode('=', $kvparts, 2);
127 $value = trim($value);
129 // Attribute key are handled case-insensitively
130 $attributes = new Requests_Utility_CaseInsensitiveDictionary();
132 if (!empty($parts)) {
133 foreach ($parts as $part) {
134 if (strpos($part, '=') === false) {
139 list($part_key, $part_value) = explode('=', $part, 2);
140 $part_value = trim($part_value);
143 $part_key = trim($part_key);
144 $attributes[$part_key] = $part_value;
148 return new Requests_Cookie($name, $value, $attributes);
152 * Parse all Set-Cookie headers from request headers
154 * @param Requests_Response_Headers $headers
157 public static function parseFromHeaders(Requests_Response_Headers $headers) {
158 $cookie_headers = $headers->getValues('Set-Cookie');
159 if (empty($cookie_headers)) {
164 foreach ($cookie_headers as $header) {
165 $parsed = self::parse($header);
166 $cookies[$parsed->name] = $parsed;