scriptygoddess

15 Apr, 2004

User Editable Comments

Posted by: Jennifer In: Scripts

I can't tell you how many times I've left a comment on a blog, and even though I had previewed it, noticed about ten spelling errors just as soon as I pressed the 'submit' button.

I decided to fix this problem, at least on my blog, by letting users modify their comments for a designated period of time. I decided to share the code in case anyone else wants to implement this. It works directly with the MySQL database, so obviously it requires a MySQL-powered Movable Type blog.

Features
* Set the number of minutes your users have to go back in and edit their comments
* Set an optional maximum number of edits for any one comment
* Comments will have the time of the edit added to the bottom (but this text will be stripped out from the user's view if he/she goes in again to edit)
* Authentication based on IP address, so users don't have to set or remember a password
* Easy to integrate with existing before/after PHP includes system you may be using
* While users are editing their comment, they see a timer that counts down how much editable time they have remaining, and pops up a warning when they have 30 seconds left (although it's a bit of a white lie, they really have 60 seconds left at that time)
* PHP code included in readme for inserting the "Edit This Comment" PHP code into your Movable Type templates so that the link will only show up next to the user's own comments, while they are still editable
* NO hacking of Movable Type code required

Requirements
* Movable Type 2.64 or higher (possibly works on older versions)
* MySQL database system being used for Movable Type installation
* PHP enabled on web server (version 4.3.0 or higher would be nice… it's been tested on 4.3.4)
* PHP enabled on entry pages (for dynamic displaying of the "Edit This Comment" link)

Download
Latest version of user_editable_comments;

Installation
1. Open up editcomment_settings.php
– Enter your database information
– Enter in your timezone offset. This is used it your server's timezone
doesn't match the timezone as set in your blog. If your server is set
to -05 GMT (eastern time) but your blog is set to -06 GMT (central
time), you'd set this to -1.
– Enter in the future location of your mt-rebuild-entry.cgi file.
It goes in the same directory as your main Movable Type CGI files.
http://www.yoursite.com/cgi-bin/mt/mt-rebuild-entry.cgi is a common
one.
– Change your comment editing settings, $minutes_until_lockout is
required, and will control the amount of time your users have to
edit their comments. $max_edits is optional, and will limit users
to a certain number of edits within the $minutes_until_lockout time.
– Set your include/insert settings. There are three types:
'before,' 'after,' and 'before_modify,' and they each come in include
and insert flavors. 'Before' is put in before the form, 'after' is put
in after, and 'before_modify' is put in right before the entry edit
textarea and will only show up when an entry is called up. This would
be a perfect place to put some clickable smilies code.
– For the JavaScript countdown timer to work, you must either place the
following line within an included $include_before file within the HEAD
section: <script type="text/javascript" src="js_countdown.js"&rt;</script&rt;
You can also put that reference within the HEAD section if you are using
$insert_before, or in either case, you can copy the contents of
js_countdown.js and paste them in the HEAD section between SCRIPT tags.
– Save the changes you have made to editcomment_settings.php

2. Decide on where you want the script to go (site root is fine, but you
may want it in a subfolder). Upload to that location the following files:
– editcomment.php
– editcomment_settings.php
– js_countdown.js

3. Upload mt-rebuild-entry.cgi to your main Movable Type CGI directory.
Set the file's permissions to match those of mt.cgi

4. You can now access the comment editing script by typing in the location
of the editcomment.php script followed by ?comment_id=X where X is a
valid comment ID. But the coolest thing to do is to add "Edit This
Comment" links next to comments before they've expired. You can do that
with this PHP code:

<?php
if ('<$MTCommentIP$>' == $_SERVER['REMOTE_ADDR']){
$user_edit_link = ' | <a href="http://www.txfx.net/php/editcomment.php?comment_id=<$MTCommentID$>">Edit This Comment</a>';
$c_date = array(<$MTCommentDate format="'%H','%M','%S','%m','%d','%Y'"$>);
for($i = 0; $i < 6; $i++){
preg_match("/(0*)(.*)/i", $c_date[$i], $match);
$c_date[$i]= (int) $match[2];
}
$date_difference = mktime() – mktime($c_date[0], $c_date[1], $c_date[2], $c_date[3], $c_date[4], $c_date[5]);
if (floor($date_difference/60) < 30) {
echo $user_edit_link;
} // end if 30 minutes has passed
} // end if IP address matches
?>

This can go anywhere within the <MTComments> </MTComments> tags, but
it will look the best if you place it like so:

<span class="comments-post">Posted by: <$MTCommentAuthorLink spam_protect="1"$> at <$MTCommentDate$>++CODE GOES HERE++</span>

And then you should be ready to go!

If you have any comments, suggestions, bug reports please email me!

40 Responses to "User Editable Comments"

1 | Blinger

April 15th, 2004 at 11:07 pm

Avatar

This looks really great, when I get some time I will add it on the weekend. I have one regular commenter who emails me asking to make corrections to his comments.

2 | David

April 16th, 2004 at 7:06 am

Avatar

Fantastic! I've been longing to provide this facility for ages. :)

There's a slight problem with the script, in that it only works if the Blog ID equals 1. You need to alter editcomments.php in order to get it working if you have multiple blogs.

Find the line that says:

$query = "SELECT comment_entry_id, comment_text FROM " . $my_mt_prefix . "comment WHERE comment_id='$query_comment_id' AND comment_email='$query_email_address' AND comment_ip='$query_ip_address' AND $query_time – UNIX_TIMESTAMP(comment_created_on) – 60

and change it to:

$query = "SELECT comment_blog_id, comment_entry_id, comment_text FROM " . $my_mt_prefix . "comment WHERE comment_id='$query_comment_id' AND comment_email='$query_email_address' AND comment_ip='$query_ip_address' AND $query_time – UNIX_TIMESTAMP(comment_created_on) – 60

Then find the line that says:

header ("Location: " . $my_rebuild_cgi . "?mode=entry&blog_id=1&entry_id=" . $row2['comment_entry_id']);

and change it to:

header ("Location: " . $my_rebuild_cgi . "?mode=entry&blog_id=" . $row2['comment_blog_id'] . "&entry_id=" . $row2['comment_entry_id']);

I think those are the only changes that need to be made.

3 | Mark J

April 16th, 2004 at 7:20 am

Avatar

Good catch, David, and a proper solution. I'll put that into the next version.

Right now I've got another "bug" I'm working on. The cgi that rebuilds only rebuilds the files directly associated with the entry. So while the individual entry archive and monthly archive will be rebuilt, the main index template won't… so people who have JavaScript expandable comments on their front page won't see any edits until someone leaves a proper comment and forces a rebuild.

The solution is obviously to have the CGI rebuild the main index as well, but I have to account for different possible names for the main index template… so the best solution might be to just have it rebuild all index templates that are marked to be rebuild. That's what happens on a normal comment submission anyway.

4 | Arvind

April 16th, 2004 at 7:52 am

Avatar

Noooo I wanted to implement this but it doens't work on mine, my ip is dynamic :-@

5 | Arvind

April 16th, 2004 at 7:55 am

Avatar

It should be change such that it checks maybe just the first bit of the IP rather than the whole thing because looking at my commentors on my blog a lot of people have dynamic IP or their Ip changes

6 | Mark J

April 16th, 2004 at 7:58 am

Avatar

Your ISP gives you a dynamic IP? That's nothing unusual. Very few people have static IPs.

This script isn't intended to allow them to edit their comments for the rest of eternity. The default setting is 30 minutes.

Their IP address isn't going to change in 30 minutes unless they force it to change.

7 | Arvind

April 16th, 2004 at 8:22 am

Avatar

Well mine does :-S, but I was able to test out the script before that happened :S

8 | Mark J

April 16th, 2004 at 8:59 am

Avatar

Updated to v0.5!

1. David's fix was implemented, for blog_ids other than "1".
2. All index templates set to rebuild are rebuilt, so comments displayed on those pages are updated.
3. <numberofupdates x> is removed before the comment is shown to the user on subsequent edits (for those who are limited commenters to a set number of edits).

For people who were using v0.4 (should be everyone… builds before that weren't really public), all you have to do to upgrade is drop in the new copies of editcomments.php and mt-rebuild-entry.cgi, and then reset the permissions on mt-rebuild-entry.cgi

Keep those bug reports, feature requests, and comments coming…

9 | David

April 16th, 2004 at 9:29 am

Avatar

Glad that fix worked out. One other thing I noticed:

In the code snippet in Step 4 above, there's the line

if (floor($date_difference/60)

Shouldn't that 30 actually be the variable $minutes_until_lockout pulled from editcomment_settings.php?

Arvind: your IP would change only if you disconnected and then dialled-up again to your ISP.

10 | Mark J

April 16th, 2004 at 9:38 am

Avatar

The 30 should be whatever you have your comments editable for (or maybe a minute less). I considered pulling the number from the editcomment_settings.php file, but I thought it might put a strain on the server, including the file so many times. But… now that you mention it… it would only get to that point in the logic flow if the IP address matches, and if you made it include_once, it would only get called once.

Let me give that a try…

11 | Mark J

April 16th, 2004 at 9:48 am

Avatar

Yep… that works fine.

<?php
if ('<$MTCommentIP$>' == $_SERVER['REMOTE_ADDR']){

@include_once 'http://www.site.com/path/editcomment_settings.php';

$user_edit_link = ' | <a href="http://www.site.com/path/editcomment.php?comment_id=<$MTCommentID$>">Edit This Comment</a>';

$c_date = array(<$MTCommentDate format="'%H','%M','%S','%m','%d','%Y'"$>);

for($i = 0; $i < 6; $i++){
preg_match("/(0*)(.*)/i", $c_date[$i], $match);
$c_date[$i]= (int) $match[2];
}

$date_difference = mktime() – mktime($c_date[0], $c_date[1], $c_date[2], $c_date[3], $c_date[4], $c_date[5]);

if (floor($date_difference/60) < $minutes_until_lockout) {
echo $user_edit_link;
} // end if 30 minutes has passed
} // end if IP address matches
?>

Once again, good call 😉

12 | Donna

April 16th, 2004 at 10:19 am

Avatar

Mark, this appears to be work well for me – nice job! When accessing the Edit This comment screen, i don't see anything in the Time Remaining box – I assume this means I'm calling the javascript file incorrectly. I did however, put the entire Javascript file in the HEAD to try and avoid this – any suggestions?

13 | Mark J

April 16th, 2004 at 10:19 am

Avatar

1. You'll want to use a system path for that @include_once ('/home/something/www/path/file.php')
2. } // end if specified minutes have passed
3. I'm going to bed.

14 | Mark J

April 16th, 2004 at 10:25 am

Avatar

It doesn't appear that you've properly put the javascript file in the head… as it isn't showing up when I view the source. E-mail me your editcomment_settings.php file (with the database password XXXed out), and I can show you how it needs to be.

15 | Mark J

April 17th, 2004 at 9:36 am

Avatar

Updated to v0.5!

Nothing was changed about the script, but one bug fix and two streamlining improvements were made by David to the PHP code that dynamically displays the "Edit This Comment" link.

1. PHP snippet gets the date/time of the comment in a much cleaner fashion.
2. Dropped the "floor()," in the compare because it is redundant.
3. $my_timezone_offset setting is respected when figuring out if to display the "Edit" link, for those who blog from a different time zone than their server.

Here is the new code:

<?php if ('<$MTCommentIP$>' == $_SERVER['REMOTE_ADDR']){

@include_once '/home/site/path/editcomment_settings.php';

$user_edit_link = ' | <a href="http://www.site.com/path/editcomment.php?comment_id=<$MTCommentID$>">Edit This Comment</a>';

$comment_time = strtotime("<$MTCommentDate format="%Y-%m-%d %H:%M:%S"$>");

$current_time = mktime() + $my_timezone_offset;

$date_difference = $current_time – $comment_time;

if ($date_difference / 60 < $minutes_until_lockout) {
echo $user_edit_link;
} // end if minutes have passed
} // end if IP address matches
?>

So if you drop that in (after replacing the URL and path in there), you'll be good to go. No need to download the new zip file.

16 | Mark J

April 17th, 2004 at 9:38 am

Avatar

Blah! Updated to v0.6

Copy / Paste is NOT my friend.

If only Jenn used my script on her site, I could edit that. 😉 (only teasing)

17 | btezra

April 19th, 2004 at 11:38 am

Avatar

~QUICK QUESTION:
does anyone know of a script I can install on my site that will require those that leave comments behind to have their comments approved before they appear? I am being habitually spammed in my comments by someone, I cannot ban the IP and rebuild the MT files fast enough sometimes…I would appreciate the help and assistance, please email me at ceb420@msn.com with any help, thnx Craig~

18 | Mark J

April 19th, 2004 at 11:45 am

Avatar

If these comments are being left automatically by a script, the best solution would be to use James Seng's scode like Jenn does here and like I do on my website. I haven't ever gotten a single comment spam (because the only way I would was if some dedicated person was doing it manually).

19 | Mike

April 20th, 2004 at 10:48 am

Avatar

A couple of things to note about this functionality regarding IP addresses:

1) People with dynamic IPs that comment can't edit a comment if they disconnect either by choice or not. I understand that it's not set up to allow them to edit a given post forever, but for argument's sake: Say there are these two people on the same ISP that read your blog with this script. One of them comments and then disconnects, and then the other connects, receives the same IP as the first person, and comes to your site. They are able to edit a comment left by the first person. This scenario isn't that likely to show up, but it is still plausible.

2) Another scenario that is more plausible involves people with CABLE/DSL routers to share a single broadband connection thoughout their homes. If they didn't set up the router correctly or used it straight out of the box, then it's likely that $_SERVER['REMOTE_ADDR'] will get the IP the router assigns their computer, which is 192.168.1.100 – 192.168.1.103 by default, depending on what port your computer is plugged in on on the router. If several readers have the same "IP" assigned, then the comments are editable for each user. This scenario only appears if the blog is on a Windows server using Apache. Linux servers have the Apache module that retrieves the computer's "proxied" address, or the one assigned to the CABLE/DSL router, which is static. Unless the server admin for the Windows server compiled and installed that module, then it shows up.

20 | Mark J

April 20th, 2004 at 12:49 pm

Avatar

Mike,
Those shortcoming were known, and considered when going into this. In fact, that is part of the reason why I require you to enter your e-mail address (which is checked against the comment in the database) before being able to edit your comment. If you share a computer with people who are so unethical that they would check your browser history to see where you've commented and go back to edit your comments, you can just use the e-mail address field as a password of sorts, and assuming you enter in a web site address (which could just be blank), the e-mail address won't be publicly shown.

The second scenario, I was not aware of, but I really think the onus is on the person who installs Apache on Windows to ensure that it is correctly configured.

So, assuming their server is configured correctly, and they use a website address to mask their e-mail address, they do not have to fear malfeasance from their family member or roommate. It's not meant to be as secure as an encrypted login, we're not transferring money here. 😉

21 | Mike

April 20th, 2004 at 1:42 pm

Avatar

No, no money here.

Neither scenario should rear its ugly head with such a short time for editing comments, I just wanted anyone that uses this to be prepared for anything that may happen, however unpleasant it may be.

22 | Mark J

April 20th, 2004 at 1:45 pm

Avatar

Full disclosure appreciated ;-).

I know one person at a site that installed this wasn't aware that it was controlled by time / number of edits variables and imagined a scenario at a site with very passionate commenting where people would sit there changing their comments hours later to make subsequent rebuttals of arguments look foolish.

That would be chaos…

23 | SV

April 20th, 2004 at 2:01 pm

Avatar

Please I need some help. If you see my blog ( http://mblog.com/forsv/) I have the Frequent Commenter section in side bar. I would like to modify that to do:

Say Tab [31]

[1] If I click on the name Tab it should go to Tab's Blog URL

[2] If I click on [31] it should go to a page with all the comments made by Tab in a format similar to when u click on category.

Can this be done or am thinking too much. Please help.

Thanks in advance.

24 | Mark J

April 20th, 2004 at 2:06 pm

Avatar

You're quite a bit off-topic, but yes, it could be done. Ask in the MT forums.

25 | Mariann

April 20th, 2004 at 8:33 pm

Avatar

Would someone kindly post the javascript sample with the include? I wasn't clear on how to insert the code into the header with the include, and I think that's why I'm getting so many errors.

Thanks. :)

26 | Mark J

April 20th, 2004 at 8:44 pm

Avatar

Gladly!

// This next section should contain HTML only, and will be echoed directly after $include_before… or if you are not using $include_before, in its place
$insert_before = <<<END

<html>
<head>
<title>
My Website
</title>
<script type="text/javascript">
<!–
[script goes here]
–>
</script>
</head>
<body>

END;

That would be the most simple form… you could also put in your CSS and any other HTML you want.

For $insert_after… remember to close the BODY and HTML tags.

27 | Mark J

April 20th, 2004 at 8:52 pm

Avatar

Gladly!

// This next section should contain HTML only, and will be echoed directly after $include_before… or if you are not using $include_before, in its place
$insert_before = <<<END

<html>
<head>
<title>
My Website
</title>
<script type="text/javascript">
<!–
[script goes here]
–>
</script>
</head>
<body>

END;

That would be the most simple form… you could also put in your CSS and any other HTML you want.

For $insert_after… remember to close the BODY and HTML tags.

28 | Mark J

April 20th, 2004 at 9:29 pm

Avatar

Updated to v0.7!

– Edited comments that are submitted blank return an error, instead of messing up the interface (thanks David). Some code was rearranged and functionized.

This will hopefully be the last bugfix release, and possibly the last release unless someone can think of a killer feature to add. If MT 3.0 (which I should be getting to test for beta soon) doesn't have a feature like this, I'll be updating it if needed when it is released.

29 | cases

May 5th, 2004 at 12:56 am

Avatar

Excellent idea. What we did is have a formula to make sure that incorrectly spelled words and grammar errors are automatically fixed. It does cause problems at times but in most cases it resolves more problems than it creates.

31 | Code Novice

April 16th, 2004 at 5:20 pm

Avatar

User Editable Comments
Via Scripty — it allows visitors to edit their own…

32 | esprit libre

April 17th, 2004 at 1:45 am

Avatar

http://www.xtrinity.com/yosoy/archives/000133.html
link to view all comments from a comment author email link to a friend user editable comments…

33 | esprit libre

April 17th, 2004 at 1:45 am

Avatar

http://www.xtrinity.com/yosoy/archives/000133.html
link to view all comments from a comment author email link to a friend user editable comments…

34 | Live in the Delirious Cool

April 17th, 2004 at 7:55 am

Avatar

Edit your own comment, why don't you?
Customizable, easy for the user to understand, and a clever innovation – this is one feature I wouldn't mind seeing in a future version of MT.

35 | Mama Write

April 17th, 2004 at 8:38 am

Avatar

User-Edited Comments
I found a very cool script this morning that allows a user to edit his own comments during a specified time period. I really like this feature for my course blog because many times, students ask me if they can…

36 | Fuddland

April 17th, 2004 at 9:02 am

Avatar

User-editable comments
User-editable comments are go!

37 | anything but ordinary

April 28th, 2004 at 4:05 pm

Avatar

Edit Comment Script
Brilliant. User Editable CommentsRequires MySQL I can't tell you how many times I've left a comment on a blog, and even though I had previewed it, noticed about ten spelling errors just as soon as I pressed the 'submit' button….

38 | Glimpse of a Grrl

May 2nd, 2004 at 7:36 pm

Avatar

Editable comments script
The Marvelous Miss Melissa asked people to install this and it looked cool, so I did. Except I'm on a network at home and apparently can't tell if it works. :p So if you have a moment, could you test…

40 | leduntitled

February 13th, 2005 at 5:03 pm

Avatar

Editable Comments
Thanks to Mark Jaquith and his script found at scriptygoddess, you will now be able to edit your comments for…

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements