Forum Moderators: open

Message Too Old, No Replies

replacing cookies

instead of adding to them

         

createErrorMsg

12:51 am on Aug 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Background: The site in question is one half 'business,' and one half blog. All of the 'business' pages use index.php (in root dir) as a template and have their content, menus, etc. brought in via PHP code. The blog pages are based on blog.php file (in /blog subdir), content, etc., likewise included via PHP.

A cookie stores the values for the currently selected stylesheet and the currently selected font size. The cookie seemed to be working fine. Style or font could be switched, then the 'business' portions of the page navigated, with the selected style remaining intact.

Problem:
When a link into the blog portion is clicked, the page loads with the other stylesheet. This ONLY happens when linking between the pages based on the index.php template and those based on the blog.php template.

Investigation revealed that the page's cookie is storing not one value for the stylesheet, but serveral. One has the path setting at the root dir. This is the one that was originally displaying on the 'business' pages. The other path is set to the blog subdir.

Granted, I'm still working on this locally, which means I've been clicking, linking, and using the style changer like mad to test it out. A 'real' user isn't likely to be switching styles on every page, but as I see it, if a user selects and alternate stylesheet while viewing one part of the site, it will not carry over to the other section.

So, DOes anyone know of a way to ensure that a cookie is overwritten on ONUNLOAD? In other words, how do I prevent the cookie from storing different values for each of the two dirs? As I understand it, one of the points behind using cookies is to make things apply across every page, so I'm sure I'm just missing some essential piece that will fix this in a flash...?

The script code:
Here is the JS code for the cookie. I assume the problem is in here somewhere.

function writeCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else {expires = "";}
document.cookie = name + '=' + value + expires;
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

window.onload = function(e) {
var cookie = readCookie("style");
var title = cookie? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
var cookie = readCookie("font");
var title = cookie? cookie : getFont();
setFont(title);
}

window.onunload = function(e) {
var title = getActiveStyleSheet();
createCookie("style", title, 365);
var title = getFont();
writeCookie("font", title, 365);
}

I apologize for the long post and thank anyone in advance for responding.

dcrombie

12:03 pm on Aug 23, 2004 (gmt 0)



Every directory can have it's own cookie which overrides the value from a parent directory. The fix is to set the path for both cookies to '/' so that they both sit in the 'root' directory and overwrite each other.

;)