PHP Doku:: Parse HTTP cookie - function.http-parse-cookie.html

Verlauf / Chronik / History: (50) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige DiensteHTTPHTTP Funktionenhttp_parse_cookie

Ein Service von Reinhard Neidl - Webprogrammierung.

HTTP Funktionen

<<ob_inflatehandler

http_parse_headers>>


Ein BenutzerBeitrag:
- Beiträge aktualisieren...
tobsn at php dot net
28.04.2007 5:38
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)



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)