alternative:
<?php
function cookie_parse( $header ) {
$cookies = array();
foreach( $header as $line ) {
if( preg_match( '/^Set-Cookie: /i', $line ) ) {
$line = preg_replace( '/^Set-Cookie: /i', '', trim( $line ) );
$csplit = explode( ';', $line );
$cdata = array();
foreach( $csplit as $data ) {
$cinfo = explode( '=', $data );
$cinfo[0] = trim( $cinfo[0] );
if( $cinfo[0] == 'expires' ) $cinfo[1] = strtotime( $cinfo[1] );
if( $cinfo[0] == 'secure' ) $cinfo[1] = "true";
if( in_array( $cinfo[0], array( 'domain', 'expires', 'path', 'secure', 'comment' ) ) ) {
$cdata[trim( $cinfo[0] )] = $cinfo[1];
}
else {
$cdata['value']['key'] = $cinfo[0];
$cdata['value']['value'] = $cinfo[1];
}
}
$cookies[] = $cdata;
}
}
return $cookies;
}
function cookie_build( $data ) {
if( is_array( $data ) ) {
$cookie = '';
foreach( $data as $d ) {
$cookie[] = $d['value']['key'].'='.$d['value']['value'];
}
if( count( $cookie ) > 0 ) {
return trim( implode( '; ', $cookie ) );
}
}
return false;
}
?>
(http://www.seo-blackhat.com/article/the-cookie-backer-php.html)
http_parse_cookie
(PECL pecl_http:0.20.0-1.4.1)
http_parse_cookie — Parse HTTP cookie
Popis
object http_parse_cookie ( [string $cookie [, int $flags [, array $allowed_extras]]] )Parses HTTP cookies like sent in a response into a struct.
Seznam parametrů
- cookie
String containing the value of a Set-Cookie response header
- flags
Parse flags
- allowed_extras
Array containing recognized extra keys. By default all unknown keys will be treated as cookie names.
Návratové hodnoty
Returns an stdClass like shown in the example on success or FALSE on failure.
Příklady
Příklad 690. Using http_parse_cookie()
<?php
print_r(http_parse_cookie("foo=bar; bar=baz; path=/; domain=example.com; comment=; secure", 0, array("comment")));
?>
Výše uvedený příklad vypíše:
stdClass Object
(
[cookies] => Array
(
[foo] => bar
[bar] => baz
)
[extras] => Array
(
[comment] =>
)
[flags] => 16
[expires] => 0
[path] => /
[domain] => example.com
)
http_parse_cookie
tobsn at php dot net
27-Apr-2007 08:38
27-Apr-2007 08:38
