homepage Welcome to WebmasterWorld Guest from 54.196.167.69
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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
greencode




msg:4458153
 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">
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script>
function SetCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}
</script>
<title></title>
</head>
<body>
<?php
if(!isset($_COOKIE["eucookie"]))
{ ?>
<div id="eucookielaw">
<a href="#" id="removecookie" onClick="SetCookie('eucookie','eucookie','100')">Accept this cookie</a>
</div>
<?php } ?>
<script>
$("#removecookie").click(function () {
$("#eucookielaw").remove();
});
</script>
</body>
</html>


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?

 

greencode




msg:4458238
 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


<?php
if(!isset($_COOKIE["eucookie"]))
{ ?>
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?

rocknbil




msg:4459079
 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