How To Create AJAX Commenting System for Static HTML Pages
I wanted to create ajax based commenting system for some html pages. The design goals were:
1. Simple to use and install
2. Database (MySQL) backed
3. Minimal changes to HTML template / files
4. Support effective (no frequent cache refreshes) html page compression / caching strategy for heavily commented sites.
An AJAX based system fits the bill. The new comments are displayed instantaneously. The html pages can be cached and / or compressed without requiring to refresh after each comment.
Spam is an issue with any commenting system. I have decided to strip html tags in the first iteration to deter spammers. As a personal decision I have decided not to capture email address. However it can be trivially added along with url if so desired.
I used Sajax as my Ajax framework. It presented few problems as Sajax is more suited for PHP / Ruby pages and not HTML. Here is my solution.
Schema:
CREATE TABLE `comments` (
`id` INT NOT NULL AUTO_INCREMENT ,
`url` VARCHAR( 255 ) NOT NULL ,
`ip` VARCHAR( 30 ) NOT NULL ,
`author` VARCHAR( 255 ) DEFAULT 'Anonymous Coward' NOT NULL ,
`location` VARCHAR( 255 ) DEFAULT 'Cyberspace' NOT NULL ,
`comment` TEXT NOT NULL ,
`when` TIMESTAMP NOT NULL,
PRIMARY KEY ( `id` ) )
);
Design notes:
The code for adding comment passed the url of the page. When the page is loaded a sajax call is made to fetch all the comment for the url of the page. New comments are displayed instantaneously as new comments are fetched after a comment has been submitted. This can additionally fetch comments from other users. However I have decided not to implement timed refresh as that may unnecessarily increase server load without adding much value to users. Validation checks are made to detect and not add duplicate comments. Comment throttling by same user can be added to prevent abuse.
Hope that helps in designing your commenting system.
Filed under CMS Software, Headline News, How To, Open Source Software, PHP, Pro Blogging, Spam Watch, Tech Note, Web, Web Services |
|
RSS 2.0 |
Trackback this Article
|
Email this Article
You may also like to read |






































June 10th, 2006 at 4:27 pm
I think that works as long as search engine indexing of the comments is not important (as you outlined in “When *NOT* to use AJAX.”
I don’t understand how stripping markup deters spammers. A spambot has no way of knowing whether you are stripping HTML/links or not. It is just going to submit the spam and hope you leave the HTML intact.
August 21st, 2006 at 12:15 pm
For antibots the best solution is generating a code automatically in an image.
August 21st, 2006 at 12:33 pm
@Brian
> I think that works as long as search engine indexing of the comments is not important
Definitely. That is also a deterrent for spammers.
> A spambot has no way of knowing whether you are stripping HTML/links or not. It is just going to submit the spam and hope you leave the HTML intact.
In my experience I have seen most spambots target sites with specific signature like those of WordPress or Movable Type. They spam based on site signature. So normally home-grown commenting system are unlikely to be invaded. When they find a popular commenting system is stripping html then they will not have any incentive to develop code for that system.
@Gunther
Do you mean spambots & CAPTCHA?
November 11th, 2007 at 6:56 pm
Thanks
November 24th, 2008 at 2:22 pm
Search engine indexing of comments not important!? How could you say that? A site needs to be semantic to get search engine visitors, simple as that.