Advertisement
phpaddict

Parse http headers

Nov 18th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.85 KB | None | 0 0
  1. <?php
  2.  
  3. function http_parse_headers($headers)
  4. {
  5.     $return_headers = array();
  6.     foreach (explode("\n", $headers) as $line)
  7.     {
  8.         if (substr($line, 0, 4) == 'HTTP')
  9.         {
  10.             continue;
  11.         }
  12.  
  13.         $is_cookie = FALSE;
  14.         if (substr($line, 0, 10) == 'Set-Cookie')
  15.         {
  16.             $is_cookie = TRUE;
  17.         }
  18.  
  19.         $expl = explode(':', $line);
  20.  
  21.         if (count($expl) == 2)
  22.         {
  23.             if ($is_cookie)
  24.             {
  25.                 if (!isset($return_headers[$expl[0]]))
  26.                 {
  27.                     $return_headers[$expl[0]] = array();
  28.                 }
  29.  
  30.                 preg_match_all("/^Set-Cookie: (.*?)=(.*?);/i", $line, $matches);
  31.  
  32.                 if (count($matches) == 3)
  33.                 {
  34.                     $return_headers[$expl[0]][$matches[1][0]] = trim($matches[2][0]);
  35.                 }
  36.                 else {
  37.                     $return_headers[$expl[0]][] = trim($expl[1]);
  38.                 }
  39.             }
  40.             else {
  41.                 $return_headers[$expl[0]] = trim($expl[1]);
  42.             }
  43.         }
  44.     }
  45.  
  46.     return $return_headers;
  47. }
  48.  
  49. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement