How to limit comments to one week after posting

I've been thinking a lot recently on the issues we'll be facing as blogging continues to grow and be successful. One of the spam fighting tools (metaphor: club, not lojack) I implemented was a restriction on comment posting to posts that have been created in the past week. A few people have written and asked how I implemented the time restriction on comments.

It is actually very simple, once you've got a SQL backend (see previous post) for your blog. Here's the script I have running out of my crontab:


use DBI;

my $dbname = 'DATABASENAME';
my $hostname = 'localhost'; # Change this if the db is on another box
my $dbuser = 'DBUSER';
my $dbpass = 'DBPASS';

$dbh=DBI->connect("dbi:mysql:database=$dbname;host=$hostname", $dbuser, $dbpass);

$dbh->do("update mt_entry set entry_allow_comments='0' where entry_created_on < date_sub(NOW(),interval 7 day)");

I run this every hour. Basically, it makes a single SQL call, which updates the entry_allow_comments column on mt_entry to turn off comment posting on entries that are older than 7 days. Use and enjoy.