Monday 10 February 2003

Comment cookie cockup

It appears that my comments are not working properly. In a P-to-P email, Michael from i·me·michael wrote:

Your comments pages required setting one cookie for my info, and since your individual archive pages don’t seem to recognize this cookie, I had to fill out the same info and have another cookie set my info when commenting from an individual page. Seems a little redundant, and there may just be something set improperly. One cookie should cover the whole thing. You might want to look into it.

Indeed I might. Allan Moult alerted me to this problem a while ago and I found that I also had to fill in my info on the individual archive pages even though it appeared properly in the popup comment window. But when I upgraded to Movable Type 2.51 and installed the latest templates, everything worked properly for me so I assumed that the changes had fixed whatever was going wrong. (I can’t recall whether or not I made a comment from both the popup window and an individual archive page.)

This thread on the Movable Type Support forum describes the opposite problem (cookie info being remembered on the archive pages but not in the popup window) but the person in question was running two different blogs with different subdomain names and was storing his individual archive pages in category-based directories.

My copy of Mozilla is storing two sets of cookies.

  • domain: .weblog.delacour.net, path: /cgi-bin/
  • domain: .weblog.delacour.net, path: /archives/

The cookie code in the individual archive entry template is identical to that in the comment listing template.

I’m curious as to whether anyone else has struck this problem when leaving a comment from an individual archive page. Naturally I’d like to fix it and would be grateful for any suggestions. If it is happening to other commenters, I’ll at least have some “evidence” to take to the MT Support Forum.

Update

Scott Hanson suggested in a comment that I should modify the cookie setting and deleting functions so that the cookies are written to the root directory i.e. change

setCookie('mtcmtauth', f.author.value, now, '', HOST, '');
setCookie('mtcmtmail', f.email.value, now, '', HOST, '');
setCookie('mtcmthome', f.url.value, now, '', HOST, '');

to

setCookie('mtcmtauth', f.author.value, now, '/', HOST, '');
setCookie('mtcmtmail', f.email.value, now, '/', HOST, '');
setCookie('mtcmthome', f.url.value, now, '/', HOST, '');

That did the trick! And it worked for Michael Hanscom too. Sincere thanks to Scott.

Given the fact that I’m running a bog-standard MT installation, I’m now wondering why Scott’s “fix” isn’t the default.

Permalink | Technorati

Comments

The cookie code does not pass a path, so the browser by default make the path that of the calling page. If you want the cookies to apply to the entire site, you can adjust the code to pass the path '/' as a parameter, as in the lines 'setCookie' and 'deleteCookie' below, i.e. the 3rd parameter for setCookie and the 2nd for deleteCookie is \'/\' instead of \'\'. Since the single quotes are escaped, it looks confusing.

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

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

Posted by Scott Hanson on 10 February 2003 (Comment Permalink)

Uh, never mind the escaped quotes. I was copying the functions from the perl code, not the actual templates. The code actually is not confusing at all, and looks like this:

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

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

Posted by Scott Hanson on 10 February 2003 (Comment Permalink)

This sounds like the same problem I've been having on my site - I just hadn't gotten to the point of being able to narrow it down, because I don't use popup windows for any of my comments. Without that as another aspect to look at, all I knew was that for some reason, at times the cookies would seem to work, and at other times they wouldn't.

Scott's explanation and fix makes a lot of sense, though - I've adjusted the code in my template and I'm rebuilding now. Hopefully that'll be the necessary trick.

Posted by Michael Hanscom on 11 February 2003 (Comment Permalink)

Update - that does seem to have been the trick. Thanks!

Posted by Michael Hanscom on 11 February 2003 (Comment Permalink)

That is a great idea Scott. Unfortunately, my MT installation is under burningbird.net/cgi-bin, while my weblogs are under weblog.burningbird.net, rdf.burningbird.net, and so on.

I had actually pulled the comment form in page because of this problem, but added back in for my weblog because of people's request. It's just unfortunate that people will have to set their cookies twice.

Unless, you have any tricks that would work with virtual domains, too?

Posted by Shelley on 11 February 2003 (Comment Permalink)

Shelly, after reading through the comments in the MovableType thread which Jonathon linked to, I believe changing the variable HOST, from this:

var HOST = '';

to this:

var HOST = 'burningbird.net';

would let one cookie work for the domain as well as any subdomains.

You would of course have to change this line in any templates where the JavaScript is used.

Speaking of which, why does everyone leave the cookie script embedded in the page. Wouldn't it make more sense to have it in an external Javascript file that any page could access? One download, it's gets cached, and pages are that many bits lighter.

Posted by michael on 11 February 2003 (Comment Permalink)

That first line should have had a MT tag in it, but I assume it was escaped. I'll use standard brackets in place of the angle ones. It should have looked a bit like this:

var HOST = '[$MTBlogHost$]';

to this:

var HOST = 'burningbird.net';

And apologies for typoing your name Shelley, I've been doing that a lot lately it seems.

Posted by michael on 11 February 2003 (Comment Permalink)

Michael, thank you very much for finding this information! With all my many weblogs and subdomains, this has been a problem. A very annoying one.

As for my name, not a worry. Most people misspell it, including my parents at times (I changed my name to Shelley when I was much younger. It was something else.)

Michael, thanks again for your help!

Posted by Burningbird on 11 February 2003 (Comment Permalink)

This discussion is now closed. My thanks to everyone who contributed.

© Copyright 2007 Jonathon Delacour