Forum Moderators: phranque

Message Too Old, No Replies

Cookies and browser cache

I always thought they were unrelated, but maybe not

         

MatthewHSE

3:00 am on Jan 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I always thought cookies and the browser cache were different animals. But it seems they're related, or at least in Firefox (haven't tested IE yet).

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

encyclo

3:06 am on Jan 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the static page is displayed from the browser cache then there is no call to the server - so the server cannot set a cookie as it is not aware that the page has been viewed.

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?

MatthewHSE

3:34 am on Jan 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a dynamic page, and I know it's getting the page from the server because the cookie contents are supposed to be echoed to the page, but in the scenario I mentioned, I'm getting an "undefined index" error. Here's some of the PHP code:

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.)

amol_v_naik

11:45 am on Jan 23, 2006 (gmt 0)

10+ Year Member



Can sombody tell me - Whats the lifespan(the number of day after which it will be deleted automatically) of adwords conversion cookie.

celgins

2:55 pm on Jan 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



MatthewHSE, cookies, cache, and history are also stored in "index.dat" files in your file system (for IE. I'm thinking Firefox does this in another way)

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.

MatthewHSE

3:42 pm on Jan 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks celgins. Actually, I did find a way around the problem I was having by rearranging my code a bit. I never did figure out the cache question, but at least the script works now.

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.