Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
double cookie mystery
I have some interest in tracking people across my website, mostly to see who is not human. I added the following code to all pages:
<txp:php>
if ( isset( $_COOKIE["bot"] ) )
$counter = 1 + (int) $_COOKIE["bot"] ;
else
$counter = 1 ;
setcookie( "bot", (string) $counter, 0 ) ;
</txp:php>
Then after I wandered about the site, clicking on some random pages, I looked at the cookies:
Cookie: bot=9; txp_login_public=...other cookies...; bot=4
The issue is repeatable. I cleared all cookies from the browser and tried again – same result: two “bot” cookies. How did I end up with the same cookie twice?
Offline
Re: double cookie mystery
Check perhaps whether your “bot” cookies are in different paths? You should be able to see that in the browser dev tools. That might explain why you have two of the same name (but in different places).
TXP Builders – finely-crafted code, design and txp
Offline
Re: double cookie mystery
Maybe it gets fixed if you update setcookie( "bot", (string) $counter, 0 ) ;
to look like setcookie( "bot", (string) $counter, 0, "/" ) ;
so the cookie is set for the whole domain.
Offline
Re: double cookie mystery
I am not so proud as to not admit that I am not sure what you mean by different paths. (Gotta love the triple negative there.) Please assume a much larger chasm of ignorance on my part.
I am looking at the cookies by enabling the Firefox web dev tools, going to Network, and looking at the request headers. I have no idea how to see the stored cookies in the browser. The request header just shows name(s) and value(s) as sent with the GET request.
Does the browser actually have two cookies and is sending both? Is php just picking the first or last?
Edit: I had only seen Jakob’s post when I wrote that. I added "/"
to the setcookie()
call, as kuopassa suggested, and the issue went away. I guess cookies are only persistent in some weird sub-tree of the URL? Cool, but I don’t need to know that.
Problem solved!
Last edited by skewray (Today 06:37:10)
Offline
Re: double cookie mystery
In Firefox, choose “Inspect (Q)”, then click on the “Storage” tab, then finally choose from the left sidebar “Cookies”. 🙂 Here’s how the cookie situation looked like before your fix:
Offline
Re: double cookie mystery
Oh, yeah, that works.
I can see you in the log. I do wonder why your browser reloaded the svg images and css for each URL, even though it should have cached them. I set both Cache-Control and ExpiresBy for those.
Offline