How can I parse $_SERVER['HTTP_COOKIE'] in pure PHP?
11:58 08 Jun 2015

I need to parse $_SERVER['HTTP_COOKIE'] string in pure PHP. I have no access to $_COOKIE or functions like http_parse_cookie (PECL).

I tried with a function like:

function ParseCookies($strHeaders)
{
$result = array();              
    $aPairs = explode(';', $strHeaders);
    foreach ($aPairs as $pair)
    {
        $aKeyValues = explode('=', trim($pair), 2);
        if (count($aKeyValues) == 2)
        {
            switch ($aKeyValues[0])
            {
                case 'path':
                case 'domain':
                    $aTmp[trim($aKeyValues[0])] = urldecode(trim($aKeyValues[1]));
                    break;
                case 'expires':
                    $aTmp[trim($aKeyValues[0])] = strtotime(urldecode(trim($aKeyValues[1])));
                    break;
                default:
                    $aTmp['name'] = trim($aKeyValues[0]);
                    $aTmp['value'] = trim($aKeyValues[1]);
                    break;
            }
        }
        $result[] = $aTmp;
    }
return $result;
}

but the explode pattern is trivial, because I can have a cookie like name="a;b;c=1;";

I need something to tokenize the string in the proper way. Any ideas?

php cookies