Cookies not remembering properly in MT3D

| 17 Comments | 8 TrackBacks

There are 4 MT3D templates where comments can be entered and edited. None of them properly save, delete, and retrieve cookies relating to comment forms:

Individual Entry Archive template:

  • rememberMe() function sets site-wide cookies (path='/'), but forgetMe() function deletes cookies for directory archive page is on (path='').

Comment Listing template:

  • Both rememberMe() and forgetMe() functions set and delete cookies for same directory archive page is on (path='').

Comment Preview template:

  • 5 out of 6 javascript functions missing from script code, including rememberMe() and forgetMe() javascript functions.

Comment Error template:

  • 5 out of 6 javascript functions missing from script code, including rememberMe() and forgetMe() javascript functions.

Although the Comment Pending template has a truncated javascript like the Comment Preview and Comment Error templates, no javascript code at all is needed as there is no comment form on this page.

All 4 of these templates should have the same, identical javascript code to handle the comment forms and the setting / deleting / retrieval of cookies.

I'm not sure if it is best to use site-wide cookies or cookies that are specific to each archive directory. On my site, I wanted a single cookie that would remember the name / e-mail / URL info on every archive page, no matter what directory the page was in. The code I have provided below will set, delete, and retrieve site-wide cookies.

Replace your existing javascript code in the <head> section of the Individual Entry Archive, Comment Listing, Comment Preview, and Comment Error templates with the following:

<script type="text/javascript" language="javascript">
<!--

var HOST = '<$MTBlogHost$>';

// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

function setCookie (name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) 
      + ((expires) ? "; expires=" + expires.toGMTString() : "") 
      + ((path) ? "; path=" + path : "") 
      + ((domain) ? "; domain=" + domain : "") 
      + ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie (name, path, domain) {
    if (getCookie(name))
        document.cookie = name + "=" 
          + ((path) ? "; path=" + path : "") 
          + ((domain) ? "; domain=" + domain : "") 
          + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function fixDate (date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)
        date.setTime(date.getTime() - skew);
}

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

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

//-->
</script>

Save your templates as you make each change. After the Individual Entry Archive template has been changed, the Individual Entry Archives will need to be rebuilt in order for the changes in the javascript to take effect.

The Comment Listing, Comment Preview, and Comment Error templates are dynamic, so no rebuilding is necessary to see the changes on them - the new javascript code will be effective as soon as the template is saved.

All that remains is to test the new templates and see if cookies are now read, set, and deleted correctly on the comment pages of your weblog. Hopefully, they will.

More than likely, the next release of MT3 will fix the javascript code in the default templates. But if you're like me and don't like having broken code on your site and don't want to wait on the next MT3 release, here it is.

Update 02-Jun-2004: Jeroen notes in the comments that not everyone should use "/" to set a site-wide cookie:

Please note that this script sets the cooky for the root '/'. This works perfectly for sites that have its own domain (example.com), but if you share a server (example.com/user) you'll need to change the functions rememberMe and forgetMe to '/user', so they set the cookie for your site only.

This sounded like a good idea to me, so I've modified the remeberMe() and forgetMe() functions to use the <$MTBlogRelativeURL$> tag for the path instead of hard-coding it to '/'. This way, the script will automatically adjust the path for blogs that are not at the root of a domain.

8 TrackBacks

An email from Dave: After doing a little research, I think I have figured out why my personal info isn't being remembered when I tell it to. Looking at the cookie manager inside Mozilla I find that the cookie that your site sets for the remember pe... Read More

How to get the cookie code working properly in MT3D. Read More

The Tweezer's Edge v3: Cookies not remembering properly in MT3D [via].Explains how to solve the problem very clearly. Useful.... Read More

I'd noticed that I was having to sign in to TypeKey every time I wanted to comment, so I took a quick trip to the MT Support Forums and found this nugget. The referenced page has some great replacement JavaScript... Read More

Parse Error from Fresh Bilge on July 12, 2004 10:01 AM

I need a Moveable Type guru. Maybe I'll visit Brad... Read More

Commenting Fixed from Bin Gregory Productions on October 25, 2004 7:35 AM

Comments should be working properly now. Please email me if you run into any problems. To comment here, you must register with TypeKey, Movable Type's commenting service. It only takes a minute, and you can then comment on any MT... Read More

Fixed it... from The Unknown Geek on March 14, 2005 11:42 AM

Thanks to the help of TweezerMan over at The Tweezer's Edge (here and here), I've got the "Remember Me" function on the comments working again... =)... Read More

Remember personal info... from Breaking Windows 2.0 on July 4, 2005 8:26 AM

An email from Dave: After doing a little research, I think I have figured out why my personal info isn't being remembered when I tell it to. Looking at the cookie manager inside Mozilla I find that the cookie that... Read More

17 Comments

Thanks a lot! I was wondering what was up with the cookies. I will add this just as soon as I have the time :P

Please note that this script sets the cooky for the root '/'. This works perfectly for sites that have its own domain (example.com), but if you share a server (example.com/user) you'll need to change the functions rememberMe and forgetMe to '/user', so they set the cookie for your site only.
I would also recommend storing the script in an external file, and load it by placing in the section to reduce the file size.

Thanks for the tip, Jeroen! I've modified the javascript to use the <MTBlogRelativeURL> tag for the path instead of '/'. This should work better for everyone.

I agree also with the tip of storing the script in an external file - I actually did this on my weblog a couple of days ago. I created a new Index template called "Javascripts", put the above javascript code in it along with my script to expand/collapse extended entries, and then just have a <script> tag link to it in the various templates. From a maintenance standpoint, having the javascript in one place is much easier to deal with.

First, let me say thanks... all three deal breaker issues I had with 30D today have been resolved by hopping around your site.

One thing I didn't find was that the code to set the fields from a saved cookie. Is this something you added via your MTCommentFields replacement? (which I am not yet using)

What were the "deal-breaker" issues you had? (Just curious...)

Yes, the code for setting the comment fields from a saved cookie is included in my MTCommentFields replacement. Look for the javascript at the very end of the template module code.

Has this not been fixed in MT3.01D? I upgraded and made a new testblog. Remember me worked ..at first. I then added some of my own scripts for image buttons and comment text formatting buttons. Remember me stopped remembering me. I then deleted all my added scripts and reverted back to the original template for comment listing, although I copied and pasted from the individual archive template rather than using the tag (before and after).. now remember me does not work. bah. I use popup commenting.

Perhaps I should look for the comment template inside the cgi-bin that is called by using the tag and try editing in there instead of on the template itself. This has been driving me nuts.

The "Remember Me" function, as a part of the MTCommentFields tag, calls on javascript located at the top of the template. The changes to MTCommentFields for MT 3.01D would not necessarily affect how well "Remember Me" does or does not work - this would depend on the javascript included in the default templates.

I haven't had time to look at the javascript in the current MT 3.01D default templates in detail, but it does appear that it has changed from what was used in MT3D.

yup.. unfortunately it still seems to be phut when using popups which is strange since it works fine with inline comments (ie: my scripts had nothing to do with it)..
Refer my entry on MT forums here for detail.
btw, this is not on my main weblog but a totally separate testblog located elsewhere.

Well, I replaced everything with the script you used above, and STILL my blasted comments won't remember people. Is there something else it could be? I'm not a developer so I might be overlooking something very simple, i just don't know. I've had several people test it for me. Not only does it not remember them, I am STILL having to approve every single commenter even if they have been approved before.

I've posted a reply to your post in the MT Forums.

Basically, your javascripts are strung out on one line inside of an HTML comment, which prevents the browser from seeing the javascript as executable code, and you appear to still be using the current version of MT-Blacklist, which is incompatible with MT3.

I've tried implementing this solution, but my site still doesn't remember me. The cookie is showing up on my computer, so it appears to be placed correctly, but can't be recalled. Any ideas?

The default Comment templates (Comment Listing, Comment Preview, Comment Error) do not include any code for actually retrieving any cookies previously set, and the MTCommentFields tag in MT 3.01D does not provide any code either (although the Individual Entry Archive template does have code to retrieve cookies). In your Comment Listing template, you'll need to add the following code immediately after the MTCommentFields tag:

<script type="text/javascript" language="javascript">
<!--
if (document.comments_form.email != undefined)
document.comments_form.email.value = getCookie("mtcmtmail");
if (document.comments_form.author != undefined)
document.comments_form.author.value = getCookie("mtcmtauth");
if (document.comments_form.url != undefined)
document.comments_form.url.value = getCookie("mtcmthome");
if (getCookie("mtcmtauth") || getCookie("mtcmthome")) {
document.comments_form.bakecookie[0].checked = true;
} else {
document.comments_form.bakecookie[1].checked = true;
}
//-->
</script>


If you want the "Remember me?" button to display the correct status on the Comment Preview and Comment Error templates, add the following code immediately after the MTCommentFields tag in those templates:

<script type="text/javascript" language="javascript">
<!--
if (getCookie("mtcmtauth") || getCookie("mtcmthome")) {
document.comments_form.bakecookie[0].checked = true;
} else {
document.comments_form.bakecookie[1].checked = true;
}
//-->
</script>

Hmmm, I'm using this replacement as well as the replacement to MTCommentFields. Still, it's not remembering anything. I posted on the MT forums, but nobody replied. If anyone can offer any advice, it would be greatly appreciated.

Looking at the script you're linking to in your comments page, the problem is pretty obvious. The code you have needs to be in an MT template if it is not already, and you need to rebuild that template in MT so the MT tags will be processed and the proper values put in their place.

Javascript will not handle this properly:
var HOST = '<$MTBlogHost$>';

But it will interpret this correctly:
var HOST = 'www.gogogadgetearl.com';

Rebuilding will also replace the multiple occurrences of <$MTBlogRelativeURL$> with '/'.

Well, I've corrected any MT tags that I had accidentally copied into the external JavaScript file. Somehow, it's still not working. If you would like to look again, I've replaced the 'comments.js' file with the recent modifications.

I'm not sure what you think the script should be remembering. There is no HTML code for the Name and Email Address fields in your comment form, and the HTML code for the URL and "Remember Me?" fields is commented out, so they don't exist either. Basically, there are no fields present anywhere in your comment form for the javascript to remember anything for.

Heh, yeah I commented it out so my users didn't get confused/frustrated. Sorry. I've un-commented it, if you would like to look at it again. Don't feel obligated, tho. I'm not trying to get you to do my work for me -- I really appreciate what you've already done. :-)