Location: PHPKode > scripts > HTTP Cookie > http-cookie/httpcookie.php
<?php
/**
 * HTTPCookie
 *
 * @access public
 * @author windylea
 * @version 0.1
 */ 
class httpcookie {

	/**
	* Check if a cookie is expired or not
	*
	* @access public
	* @param string $time The value of the 'expires' key presented in the "Set-Cookie" header
	* @return bool True if the cookie is expired or malfunctioned ( not in the format "Wdy, DD-Mon-YYYY HH:MM:SS GMT" )
	*/ 

	function is_expired_cookie($time) {
		$time_conv = strtotime($time);
		$time_now = time();
		if(is_int($time_conv) && $time_conv > $time_now) return false;
		$scan = sscanf($time, "%3s, %2d-%3s-%4d %2d:%2d:%2d %s", $wday, $d, $mo, $y, $h, $mi, $s, $zone);
		if(empty($y)) return true;
		$ltime = localtime();
		$lyear = 1900+$ltime[5];
		if($y > $lyear) return false;
		$months = array(
			'jan' => 1,  'feb' => 2,  'mar' => 3,
			'apr' => 4, 'may' => 5, 'jun' => 6,
			'jul' => 7, 'aug' => 8, 'sep' => 9, 
			'oct' => 10, 'nov' => 11, 'dec' => 12
		);
		$mo = $months[strtolower($mo)];
		$lmonth = 1+$ltime[4];
		if($y == $lyear && $mo > $lmonth) return false;
		if($y == $lyear && $mo == $lmonth && $d > $ltime[3]) return false;
		if($y == $lyear && $mo == $lmonth && $d == $ltime[3] && $h > $ltime[2]) return false;
		if($y == $lyear && $mo == $lmonth && $d == $ltime[3] && $h == $ltime[2] && $mi > $ltime[1]) return false;
		if($y == $lyear && $mo == $lmonth && $d == $ltime[3] && $h == $ltime[2] && $mi == $ltime[1] && $s > $ltime[0]) return false;
		return true;
	}

	/**
	* Parse cookies from HTTP headers
	*
	* @access public
	* @param array $headers Contains HTTP headers to be parsed for "Set-Cookie" headers
	* @param string $url The original URL returned the HTTP headers ( $headers )
	* @param array $cookies Existing array which was returned from this class ( specify it to update it ), default is an empty array
	* @return false|array False if $headers/$url is empty, array on success
	*/ 

	function parse($headers, $url, $cookies = array()) {
		if(empty($url) || empty($headers)) return false;
		$output = parse_url(trim($url));
		if(empty($output['path'])) $output['path'] = '/';
		foreach($headers as $header) {
			if(preg_match('/^Set-Cookie:/i', $header)) {
				$header = preg_replace('/^Set-Cookie:/i', '', trim($header));
				$elements = explode(';', $header);
				$array = array('secure' => 0);
				foreach($elements as $element) {
					list($name, $content) = explode('=', trim($element), 2);
					$name = trim($name);
					if(!empty($name)) {
						switch($name) {
							case 'domain':
							case 'path':
							case 'comment':
								$array[$name] = trim($content);
								break;
							case 'expires':
								$array['expires'] = trim($content);
								break;
							case 'secure':
								$array['secure'] = 1;
								break;
							default:
								$cookie_name = $name;
								$array[$name] = trim($content);
								break;
						}
					}
				}
				if(empty($array['domain'])) $array['domain'] = $output['host'];
				if(empty($array['path'])) $array['path'] = $output['path'];
				$cookies[$array['domain']][$array['path']][$cookie_name] = $array;
			}
		}
		return $cookies;
	}

	/**
	* Rebuild cookies
	*
	* @access public
	* @param array $cookies Existing array which was returned from this class
	* @param string $url The target URL to build cookies for
	* @param bool $expires Force selecting not expired cookies only, default is false
	* @return false|string False if $cookies/$url is empty, string on success
	*/ 

	function build($cookies, $url, $expires = false) {
		if(empty($url) || empty($cookies)) return false;
		$output = parse_url(trim($url));
		if(empty($output['path'])) $output['path'] = '/';
		$host_length = strlen($output['host']);
		$path_length = strlen($output['path']);
		
		$scheme = $output['scheme'];
		foreach($cookies as $domain => $contents) {
			$result = $host_length - strlen($domain);
			if($domain == $output['host'] || ($result > 0 && substr($output['host'], $result) == $domain && $domain[0] == '.') || ('.'.$output['host'] == $domain)) {
				foreach($contents as $path => $cookie) {
					if($path_length >= strlen($path) && substr($output['path'], 0, strlen($path)) == $path) {
						foreach($cookie as $name => $value) {
							if((!$expires || ($expires && !empty($value['expires']) && !httpcookie::is_expired_cookie($value['expires']))) && (($scheme == 'http' && $value['secure'] == 0) || $scheme == 'https')) $return.= $name.'='.$value[$name].'; ';
						}
					}
				}
			}
		}
		return trim($return);
	}

	/**
	* Save cookies for later use by using serialize() function
	*
	* @access public
	* @param array $cookies Existing array which was returned from this class
	* @param string $filename The file for the cookies to be written to, default is empty
	* @return bool|string
	*/ 

	function save($cookies, $filename = null) {
		$cookie = serialize($cookies);
		if(empty($filename) || !is_string($filename)) return $cookie;
		if(is_writable($filename) || !file_exists($filename)) {
			if($handle = fopen($filename, 'w+')) {
				fwrite($handle, $cookie);
				fclose($handle);
				return true;
			}
		}
		return false;
	}

	/**
	* Load cookies from saved cookies
	*
	* @access public
	* @param string $string
	* @param bool $filename
	* @return array|false Array on success
	*/ 

	function load($string, $filename = false) {
		if(!$filename) {
			$cookies = unserialize($string);
			return $cookies;
		}
		if(is_readable($string)) {
			if($handle = fopen($string, 'r')) {
				$filesize = filesize($string);
				if($filesize > 0) {
					$contents = fread($handle, $filesize);
					fclose($handle);
					$cookies = unserialize($contents);
					return $cookies;
				}
			}
		}
		return false;
	}
}

if(!function_exists('http_build_cookies')) {
	function http_build_cookies() {
		$args = func_get_args();
		return call_user_func_array(array('httpcookie', 'build'), $args);
	}
}

if(!function_exists('http_parse_cookies')) {
	function http_parse_cookies() {
		$args = func_get_args();
		return call_user_func_array(array('httpcookie', 'parse'), $args);
	}
}

if(!function_exists('http_save_cookies')) {
	function http_save_cookies() {
		$args = func_get_args();
		return call_user_func_array(array('httpcookie', 'save'), $args);
	}
}

if(!function_exists('http_load_cookies')) {
	function http_load_cookies() {
		$args = func_get_args();
		return call_user_func_array(array('httpcookie', 'load'), $args);
	}
}
?>
Return current item: HTTP Cookie