| Bug #32802 | General cookie overrides more specific cookie (path) | ||||
|---|---|---|---|---|---|
| Submitted: | 23 Apr 2005 2:19pm UTC | Modified: | 17 May 2005 8:42pm UTC | ||
| From: | ast at gmx dot ch | Assigned to: | ilia | ||
| Status: | Closed | Category: | HTTP related | ||
| Version: | 4CVS-2005-05-17 | OS: | * | ||
| Votes: | 2 | Avg. Score: | 5.0 ± 0.0 | Reproduced: | 2 of 2 (100.0%) |
| Same Version: | 2 (100.0%) | Same OS: | 1 (50.0%) | ||
[23 Apr 2005 2:19pm UTC] ast at gmx dot ch
[24 Apr 2005 3:21am UTC] ast at gmx dot ch
Here is a fix for my application.
/**
* Fix the superglobal $_COOKIE to conform with RFC 2965
*
* We don't use $_COOKIE[$cookiename], because it doesn't conform to
RFC 2965 (the
* cookie standard), i.e. in $_COOKIE, we don't get the cookie with
the most specific path for
* a given cookie name, we get the cookie with the least specific
cookie path.
* This function does it exactly the other way around to conform
with the RFC.
*
* This function reevaluates the HTTP Cookie header and populates
$_COOKIE with the correct
* cookies.
*
* @static
*/
function fixCookieVars() {
if (isset($_SERVER['HTTP_COOKIE']) &&
!empty($_SERVER['HTTP_COOKIE'])) {
$allCookies = explode(';', $_SERVER['HTTP_COOKIE']);
/*
* Get rid of less specific cookies if multiple cookies with the same
NAME
* are present. Do this by going from left/first cookie to right/last
cookie.
*/
/* Reset the $_COOKIE array */
$_COOKIE = array();
/* Repopulate it, but now correctly */
foreach ($allCookies as $cookie) {
/* Split NAME [=VALUE], value is optional */
$cookie = explode('=', $cookie);
$key = preg_replace('|\s|', '', $cookie[0]);
$value = isset($cookie[1]) ? $cookie[1] : '';
if (!isset($_COOKIE[$key])) {
$_COOKIE[$key] = $value;
}
}
}
}
}
[24 Apr 2005 2:01pm UTC] sniper@php.net
Reproduce script:
<?php
setcookie("TestCookie", "Value1", time()+3600, "/test/");
setcookie("TestCookie", "Value2", time()+3600, "/");
if (!isset($_COOKIE['TestCookie'])) {
header("Location: {$_SERVER['PHP_SELF']}");
exit();
}
echo '<pre>';
var_dump($_COOKIE);
var_dump($_SERVER['HTTP_COOKIE']);
echo '</pre>';
?>
[17 May 2005 8:42pm UTC] iliaa@php.net
This bug has been fixed in CVS. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. Thank you for the report, and for helping us make PHP better.
