开发者

PHP Get Cookies From Header String

I have a string variable containing the header response. like this

This string is grabbed by sending a request through fsockopen() and fetching the header by

$nn= "\r\n";
    do {
        $header .= fgets ( $fp, 16384 );
    } while ( strpos ( $header, $nn . $nn ) === false );

output:

$header = "HTTP/1.0 200 OK Date: Tue, 13 Sep 2011 07:57:08 GMT Last-Modified: Tue, 13 Sep 2011 07:57:08 GMT 
Set开发者_开发知识库-Cookie: EUID=face313a-dddd-11e0-a694-00000aab0f6c; 
expires=Mon, 08 Sep 2031 07:57:08 GMT; 
path=/; 
domain=.mysite.com; 
Set-Cookie: MIAMISESSION=face28e8-dddd-11e0-a694-00000aab0f6c:3493353428; 
path=/; 
domain=.mysite.com; 
Set-Cookie: USER_STATE_COOKIE=664d9ed4a110fa1deb0dc4cc2d7e76fa00a75a0b5c9b3fa8; 
path=/; 
domain=.mysite.com; 
Set-Cookie: MIAMIAUTH=f8d13d07cc4e8b0ef9904f5ce40367ce44c989661956ef1cd2804726b0dd5508db661a8f9582df19bc954c57d0f2c293760e4d5ddda88d556dbcfae8dd80a14c8378a2374eb5aeb636e8c4ca7849e48792f5588ad73b3d79ce55afb95660b5052d349082e88b29653d3df674e0fd328f75ad0edb5b63a434c0b7ce44b5c338a0676d6d6648b12e8e7f9570cf24b3ef7b5dfe647f2f36a62114ae5ef23b0a3f6ad15cabba99bf945243553a5329457337884d5671141f6cc438302813801fe2527ad29c1c4e76aedacded581bea2a1b6158b7d8ec3b09e2e6c9a87495f9e6d33188bdc5074d10564d30494e1b1f6af58ab96d11dd4817a0fc17619853792c8785; 
path=/; 
domain=.mysite.com; 
HttpOnly; 
Set-Cookie: SY_REMOTEACCESS=;
Content-Type: text/html Expires: Tue, 01 Jan 1980 04:00:00 GMT X-RE-Ref: 0 -77259901 X-Cache: MISS from 192.168.1.1 X-Cache-Lookup: MISS from 192.168.1.1 Via: 1.0 192.168.1.1 (squid) Connection: close";
";

now I want to get all cookies to an array

$cookie[1] = "EUID=face313a-dddd-11e0-a694-00000aab0f6c";
$cookie[2] = "MIAMISESSION=face28e8-dddd-11e0-a694-00000aab0f6c:3493353428";
...

how can I do that? Is that a regular expression job or is there a class or function to make this easier?


Another way to do it:

    $headerCookies = explode('; ', getallheaders()['Cookie']);

    $cookies = array();

    foreach($headerCookies as $itm) {
        list($key, $val) = explode('=', $itm, 2);
        $cookies[$key] = $val;
    }
    
    print_r($cookies); // assoc array of all cookies


I used the following code:

$domain = "www.google.com";

// open the socket
$f = @stream_socket_client($domain . ":80", $err, $errstr, 30, STREAM_CLIENT_CONNECT);

if(!$f)
  die("Error: " . $err . $errstr);

// prepare the HTTP request headers
$request_headers  = "GET / HTTP/1.1" . "\r\n";
$request_headers .= "Host: " . $domain . "\r\n";
$request_headers .= "Connection: close" . "\r\n";
$request_headers .= "\r\n";

// send the HTTP request
fputs ($f, $request_headers);

// read everything from the socket
$buf = "";
while (!feof($f)) {
  $buf .= fgets ($f, 8192);
}

// close the socket
fclose($f);

// split the headers and the body
$response = preg_split("|(?:\r?\n){2}|m", $buf, 2);
if(!isset($response[1]))
  $response[1] = "";

$headers = array();
$content = $response[1];

$out = preg_split("|(?:\r?\n){1}|m", $response[0]);
foreach($out as $line){
  @list($key, $val) = explode(": ", $line, 2);
  if ($val != null) {
    if(!array_key_exists(strtolower($key), $headers))
      $headers[strtolower($key)] = strtolower(trim($val));
  } else 
    $headers[] = $key;
}

// now you have the response headers in an array
print_r($headers);

Next is to grab each cookie stored as a string value and to parse it.To achieve this, I used a function that I copied from Zend Framework fromString(): http://www.sourcexref.com/xref/moodle/nav.html?lib/zend/Zend/Http/Cookie.php.source.html#l274 .

Good luck.

P.S. This line $headers[strtolower($key)] = strtolower(trim($val)); will override the value of an header parameter if this command was sent twice by the server.

I.e.:

...
Connection: close
Expires: -1
Connection: close
Cache-control: private, max-age=0
...


Use http_parse_headers() function


Another short way:

if(preg_match_all('/Set-Cookie:[\s]([^;]+)/', $header, $matches)){
    $cookies = $matches[1];
}

it will give you exactly what you wanted

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜