Requests_Cookie::parse() WordPress Method

This function parses the HTTP cookie header into an associative array of cookie names and values. It will also decode the cookies if they are encoded with RFC 6265's encoding.

Requests_Cookie::parse( $string,  $name = '',  $reference_time = null ) #

Parse a cookie string into a cookie object


Description

Based on Mozilla’s parsing code in Firefox and related projects, which is an intentional deviation from RFC 2109 and RFC 2616. RFC 6265 specifies some of this handling, but not in a thorough manner.


Top ↑

Parameters

(string)(Required)Cookie header value (from a Set-Cookie header)


Top ↑

Return

(Requests_Cookie) Parsed cookie object


Top ↑

Source

File: wp-includes/Requests/Cookie.php

	public static function parse($string, $name = '', $reference_time = null) {
		$parts   = explode(';', $string);
		$kvparts = array_shift($parts);

		if (!empty($name)) {
			$value = $string;
		}
		elseif (strpos($kvparts, '=') === false) {
			// Some sites might only have a value without the equals separator.
			// Deviate from RFC 6265 and pretend it was actually a blank name
			// (`=foo`)
			//
			// https://bugzilla.mozilla.org/show_bug.cgi?id=169091
			$name  = '';
			$value = $kvparts;
		}
		else {
			list($name, $value) = explode('=', $kvparts, 2);
		}
		$name  = trim($name);
		$value = trim($value);

		// Attribute key are handled case-insensitively
		$attributes = new Requests_Utility_CaseInsensitiveDictionary();

		if (!empty($parts)) {
			foreach ($parts as $part) {
				if (strpos($part, '=') === false) {
					$part_key   = $part;
					$part_value = true;
				}
				else {
					list($part_key, $part_value) = explode('=', $part, 2);
					$part_value                  = trim($part_value);
				}

				$part_key              = trim($part_key);
				$attributes[$part_key] = $part_value;
			}
		}

		return new Requests_Cookie($name, $value, $attributes, array(), $reference_time);
	}

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.