Forum Moderators: phranque
The problem I'm having is that I can set a cookie, then delete the cookie, and unless I also empty the browser cache, the cookie won't be set again when I visit the page that's supposed to set it.
This doesn't seem right to me. I would have expected the cookie to be set independently of the browser cache.
Am I falling into a common error, or have I stumbled upon something new? It must be one or the other, and I doubt it's the latter... ;)
Thanks,
Matthew
Is the page static or generated, and how is the cookie set? When you view the page info (Ctrl+I) in Firefox what does the
Expires: setting say?
if (!isset($_COOKIE['MyCookie'])) {
$CookieValue = 'somevalue';
header("Set-Cookie: MyCookie=$CookieValue; path=/; domain=$_SERVER[HTTP_HOST]; expires=time()+3600");
header("HTTP/1.1 301 Moved Permanently");
header("Location: $_SERVER[PHP_SELF]");
}
For the sake of simplicity, I cut out some code that isn't really relevant. However, just to give the full picture of what's going on, the script checks for the presence of a query string. If it finds one, it writes the cookie, then does a 301 redirect to the same page without the query string.
I'm using the following code on the page to be able to view the cookie contents:
echo $_COOKIE['MyCookie'];
The result is that, if I call the page with a query string, it writes the cookie, then redirects to the same page minus the query string. I can then see the contents of the cookie written to the page without even needing to refresh (since the cookie was sent prior to the redirect).
So the full scenario is that the cookie will be set on the first visit to the page. If I delete the cookie but NOT my browser cache, the cookie won't be set again until I DO empty the cache. Yet, I'm not viewing a cached version of the page since the
echo $_COOKIE.... line returns an error on subsequent pageloads after deleting the cookie - so the browser is obviously grabbing the page from the server. I'm baffled.
(On a separate note, I seem to be doing something wrong with the cookie expiration, too - it always gets set to expire at the end of the browser session rather than the current time plus 3600 seconds.)
Every time a website creates a cookie on your machine, IE will insert an entry in the cookies "index.dat" file that associates a particular homepage with the cookie. The same thing happens for cache and history.
The next time you visit that homepage, IE finds the cookie through the "index.dat" file. This may occur, even after you've deleted your cookies.
This has long been a privacy issue for many folks, but I don't think anything has ever come of it.
I think it has something to do with the browser caching the 301 redirect info. My script took a query-stringed URL, sent a cookie based on the query string, and then did a 301 redirect to the same page minus the query string. But, if the 301 redirect was cached by the browser, it would go to the non-query string page without even requesting the page with the query string, thereby bypassing the cookie-setting code entirely. I think that's why the cookie wasn't getting set again until the cache was emptied.