Cookies finally fixed!

This entry was published at least two years ago (originally posted on February 10, 2003). Since that time the information may have become outdated or my beliefs may have changed (in general, assume a more open and liberal current viewpoint). A fuller disclaimer is available.

Thanks to a comment answering a question of Jonathon’s, I’m about 95% sure that the cookies for djwudi.com are finally being set correctly, so that the comment forms will actually remember your information, instead of just teasing you with the possibility that they might remember one day if they feel like it.

Turns out it was just six characters I needed to add to the JavaScript code in the template. Sheez.

In Scott’s words from his post on Jonathon’s page (emphasis mine):

The cookie code does not pass a path, so the browser by default make the path that of the calling page.

In other words, the code really was setting the cookies — but only for one page at a time. This led to some of the confusion, where both Dyanna and I thought it was working at first, then it wasn’t working again. We’d leave a comment and the cookie would get set for that page, so when the page reloaded, our info was there, and it looked like it worked. But then, as soon as we went to another page, the info was lost again.

Luckily, the fix is easy enough:

If you want the cookies to apply to the entire site, you can adjust the code to pass the path ‘/’ as a parameter…

function rememberMe (f) {
var now = new Date();
fixDate(now);
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
setCookie('mtcmtauth', f.author.value, now, <strong>'/'</strong>, HOST, '');
setCookie('mtcmtmail', f.email.value, now, <strong>'/'</strong>, HOST, '');
setCookie('mtcmthome', f.url.value, now, <strong>'/'</strong>, HOST, '');
}

function forgetMe (f) {
deleteCookie('mtcmtmail', <strong>'/'</strong>, HOST);
deleteCookie('mtcmthome', <strong>'/'</strong>, HOST);
deleteCookie('mtcmtauth', <strong>'/'</strong>, HOST);
f.email.value = '';
f.author.value = '';
f.url.value = '';
}

The bolded bits in the above code were the only parts of my individual entry template I had to change — once those were fixed, I rebuilt my site, and things actually seem to be working now!

The reason that this problem doesn’t affect most people is that by default, MT stores all archives in a single directory. Since every archive page is pulled from the directory the cookie is set for, it works fine.

I’ve adjusted my archives to fall into directories based upon when they are posted — for instance, this entry will end up in the /archives/2003/02/10/ directory. So, when the cookies are only set for one directory at a time, it causes problems. By adding the slash to the above lines of JavaScript, the cookie is set for the entire site, and will be read for every page, no matter which directory it lives in.

Much better.