1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
<?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 = ( strpos$line';' ) !== false ) ? explode';'$line ) : array( $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 ) > ) {
            return 
trimimplode'; '$cookie ) );
        }
    }
    return 
false;
}

?>