homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Cookie not being set for all pages

5+ Year Member

Msg#: 4458151 posted 3:03 pm on May 26, 2012 (gmt 0)

I'm using the following code to set a cookie:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
function SetCookie(c_name,value,expiredays)
var exdate=new Date()
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
{ ?>
<div id="eucookielaw">
<a href="#" id="removecookie" onClick="SetCookie('eucookie','eucookie','100')">Accept this cookie</a>
<?php } ?>
$("#removecookie").click(function () {

So when the user clicks the "Accept this cookie" link it will set the cookie and close the div. If the user comes back to the page then it checks if that cookie exists and then doesn't show the link if it does exist.

The problem I have is that I have this in the header of my site and if I visit the homepage directly the cookie sets the path as "/" and so further pages do not show the "Accept" cookie link. If however

If however I navigate to a page off of the root i.e. /about then it will set the cookie but then going back to the homepage the "Accept" link remains and I have to click it again.

Not quite sure what I'm doing wrong?



5+ Year Member

Msg#: 4458151 posted 8:05 pm on May 26, 2012 (gmt 0)

I've managed to resolve a bit of this by changing the code to:

document.cookie=c_name+ "=" +escape(value)+";path=/"+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())

Notice the "path" addition.

What I'm struggling with now is getting this bit to work

{ ?>
Show this here
<?php } ?>

The problem is the user has to refresh the page before this takes affect, I'm presuming because the page is still in the cache.

Any ideas?


WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4458151 posted 4:47 pm on May 29, 2012 (gmt 0)

Well that's not why it does that. A cookie is set in the browser and can't be read until the next page load because it's a single direction communication. You can't set it and read it at the same time.

You're probably going to need to do a location reload/redirect to the same page after setting a cookie.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved