HTTP 函数
在线手册:中文  英文


HTTP 函数
在线手册:中文  英文

用户评论:

ghpille at hotmail dot com (2012-04-05 10:55:18)

I don't think you can split the cookie-line on ";", since the semicolon is allowed withing double-quotes, eg.:
Set-Cookie: COOKIE1="a;b;c"; path=/xyz; Max-Age=100

marcoigarapava at gmail dot com (2011-04-06 20:56:49)

Here's a good way to get cookies from a page and then resubimit the data to that page (or acess any other page using the cookies gotten).

<?php

//SOME DATA TO POST
$postData = array
(
'user' => $login,
'pass' => $pass
);

//FIRST POST
$post1 http_post_fields('http://example.com'$postData);

//GET THE COOKIES STORED TO THE ARRAY $cookie
preg_match('/Set-Cookie: (.*)\b/'$post1$cookie);
$cookie = (array) http_parse_cookie($cookie[1]);
$cookie = array( 'cookies' => $cookie["cookies"]);

//POST DATA NOW WITH THE COOKIE
$post2 http_post_fields('http://example.com'$postData, array(), $cookie);
?>

tobsn at php dot net (2007-04-27 20:38:56)

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 ) > ) {
                        return 
trimimplode'; '$cookie ) );
                }
        }
        return 
false;
}

?>

(http://www.seo-blackhat.com/article/the-cookie-backer-php.html)

易百教程