/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Cookie won’t stay set

I know I’m not supposed to cross post, but I let my other thread up for a day in the Javascript forum with no success and I think it might be because it’s being looked at by the wrong audience. Wonder if some the PHP experts can spot a flaw somewhere that I have missed.

I’m thinking this has something to do with headers. I am using JS to send an ajax request to set a cookie when a user clicks a div. The request gets sent and the server-side script get’s executed properly.

This is the response:

[code]
Date Tue, 02 Mar 2010 14:56:56 GMT
Server Apache/2.2.14 (Win32) PHP/5.2.10
X-Powered-By PHP/5.2.10
Set-Cookie jakscolor=green; expires=Tue, 01-Mar-2011 14:56:56 GMT
Content-Length 0
Keep-Alive timeout=5, max=100
Connection Keep-Alive
Content-Type text/html
[/code]

All well and good, I can see that my cookie has been set (jakscolor).
But when I refresh and the page and do print_r($_COOKIE) I get nada, nil, zip.

Interestingly enough, I found out that if I execute the script two of three times in the same session, I can see by looking at the request header of the subsequent request to the response I showed above:

[code]
Host myserver.dev
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8
Accept */*
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
Connection keep-alive
Content-Type application/x-www-form-urlencoded
X-Requested-With XMLHttpRequest
Referer http://myserver.dev/minutemuffler/admin/
Cookie jakscolor=green; __utma=260484881.821254434.1264890648.1265124054.1266440794.7; __utmz=260484881.1264890648.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); PHPSESSID=crk4ov8e3gfmgipgfr2252n0e2
[/code]

that my last cookie was set (as can be noted by jakscolor=green in the Request Header).

But it’s not actually getting set, because when the page is refreshed, like I said, $_COOKIE is empty…..WTF?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperMar 03.2010 — Set cookie path. Use "/" if not explicitly defined.

Post cookie generating code if above does not help. Also, normally a header refresh needs to take place (i.e. next page), but since you're using Ajax and sending headers that way, it should have worked with cookie path, expiration set to future, value and name. If it still doesn't work, clear all cookies for that domain to ensure your browser hasn't corrupted them locally.

-jim
Copy linkTweet thisAlerts:
@aj_nscauthorMar 03.2010 — It's working now, although I'm not sure if that did it. I must've changed something in my sleep and forgotten about it, thanks anyway for the reply SrWebDeveloper!
Copy linkTweet thisAlerts:
@SrWebDeveloperMar 03.2010 — Glad it's workin' - whatever it was. Cheers. ?
×

Success!

Help @aj_nsc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...