Bo parsowanie nagłówków powinno wyglądać tak:
public function parseHeaderLine($headerLine)
{
$headerLine = trim($headerLine, "\r\n");
if ('' == $headerLine) {
// empty string signals the end of headers, process the received ones
if (!empty($this->headers['set-cookie'])) { $cookies = is_array($this->headers['set-cookie'])?
$this->headers['set-cookie']:
array($this->headers['set-cookie']); foreach ($cookies as $cookieString) {
$this->parseCookie($cookieString);
}
unset($this->headers['set-cookie']); }
$this->headers[$k] = implode(', ', $this->headers[$k]); }
}
} elseif (preg_match('!^([^\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]+):(.+)$!', $headerLine, $m)) { // string of the form header-name: header value
if (empty($this->headers[$name])) { $this->headers[$name] = $value;
} else {
$this->headers[$name] = array($this->headers[$name]); }
$this->headers[$name][] = $value;
}
$this->lastHeader = $name;
} elseif (preg_match('!^\s+(.+)$!', $headerLine, $m) && $this->lastHeader) { // continuation of a previous header
if (!is_array($this->headers[$this->lastHeader])) { $this->headers[$this->lastHeader] .= ' ' . trim($m[1
]); } else {
$key = count($this->headers[$this->lastHeader]) - 1; $this->headers[$this->lastHeader][$key] .= ' ' . trim($m[1
]); }
}
}
nastomiast parsowanie cookie tak:
protected function parseCookie($cookieString)
{
'expires' => null,
'domain' => null,
'path' => null,
'secure' => false
);
if (!strpos($cookieString, ';')) { // Only a name=value pair
$pos = strpos($cookieString, '='); $cookie['name'] = trim(substr($cookieString, 0
, $pos)); $cookie['value'] = trim(substr($cookieString, $pos + 1
));
} else {
// Some optional parameters are supplied
$elements = explode(';', $cookieString); $pos = strpos($elements[0], '='); $cookie['name'] = trim(substr($elements[0
], 0
, $pos)); $cookie['value'] = trim(substr($elements[0
], $pos + 1
));
for ($i = 1; $i < count($elements); $i++) { if (false === strpos($elements[$i], '=')) { $elName = trim($elements[$i]); $elValue = null;
} else {
}
if ('secure' == $elName) {
$cookie['secure'] = true;
} elseif ('expires' == $elName) {
} elseif ('path' == $elName || 'domain' == $elName) {
} else {
$cookie[$elName] = $elValue;
}
}
}
$this->cookies[] = $cookie;
}
Ten kod - w odróżnieniu od tego co podano wyżej - zadziała zawsze.
Jeśli ktoś ma wolne pół dnia na analizowanie linia po linii, co się w tym kodzie dzieje, to śmiało.
Ja mam ciekawsze zajęcia (na przykład tworzenie softu który dla odmiany DZIAŁA!).