Jim Westergren
A blog about me, my projects, SEO, Web Development and Personal Development.
"If we did all the things we are capable of, we would literally astound ourselves." - Thomas A. Edison

A new approach to block web spam

This article updated on the august 11, 2010.

Around half a year ago or so I had a unique idea (at least I had never heard or read about it before) on how to block web spam. It is very simple, yet very powerful.

Normally comment spam, false registrations and other such spam are being submitted by bots (evil programs or scripts) that send their spam to millions of web sites and blogs automatically. This is a major problem on the internet. Check out State of Web Spam written on the April 22nd, 2010 by Akismet.com.

My technique does not:

My technique:

The solution is so simple you could laugh on it – but yet I have never heard about it before despite all my reading on the subject.

The solution?

Check how long time it took from the page is loaded until the form is submitted. If less than 10 seconds (or whatver you choose) block with a message. :)

Example for WordPress

Open up the file comments.php in your theme and just before </form> add this:

<input type="hidden" name="time" value="<?php echo time(); ?>" />

Then open up /wp-comments-post.php in the root and on the top after <?php add:

$minimum_seconds = 10;
if (!isset($_POST['time']) or $_POST['time'] + $minimum_seconds > time()) {
	die("Error: blog post read, comment written and submitted in less than ".$minimum_seconds." seconds. This is meaning that you are either a spam bot or you didn't read the post and wrote a good comment before submitting it. <a href=\"javascript: history.go(-1)\">Go back and try again if you are not a bot and have good intentions</a>.");
}

The above code should work on pretty much all PHP powered websites. Just add the hidden input value with the time and then the code on the page submitted to with POST.

111 days of testing (april 22 – august 11, 2010)

I added the above to this blog and deactivated both WP-SpamFree Anti-Spam (over 6K lines of PHP code) and Bad Behavior (insane logging into database) to see how it is going. I also added some small logging.

Now I am after 111 days I update this article with the test results.

Spam comments blocked: 10257
Spam not blocked: 547
Good comments: 245

The spam that was not blocked was mostly from bots and catched by Akismet and some was from humans that commented something stupid just to try to get a link.

That is 94,94% of the spam blocked with 5 lines of code. Not bad.

Caveats

Does not work with caching enabled. ;(


I am using this same technique (along with others) on the contact forms on my free website system N.nu. There are more than 1K 4K active websites and this is one of the techniques I use to block all messages from bots without compromising the user experience.

Let me know what you guys think.

Tweet this! Boomark This! Subscribe to the RSS feed
About the Author Jim Westergren Jim Westergren is a company owner from Sweden who lives together with his wife and son. Some of his interests are SEO, web development, writing and taking photos. Read his full about page or contact him. Follow Jim on Twitter or subscribe to this blog.
In Swedish: presentation, blog.
33 responses »Leave a comment
  1. Killjoy
    said on April 22, 2010 at 3:26 pm

    Not entirely unique, but a very good idea. I have been using this technique since 2001, but haven’t told anyone. Don’t tell anyone I’m doing so. Please destroy this message after you have read it.

  2. Jim Westergren
    said on April 22, 2010 at 3:38 pm

    Did it work well for you? How many seconds did you use and many percent spam did you stop?

  3. Ostnan Mk
    said on April 22, 2010 at 5:44 pm

    A very good idea,My phone notification a lot messages on my mail.They all come from you.The original is because you are testing.
    I opened your n.nu on a website and my friends.
    Perhaps, you could easily find us, haha

  4. Jim Westergren
    said on April 23, 2010 at 8:04 am

    Hi Ostnan,

    Sorry for those test comments – I had to test it :)

    Nice that you are trying out N.nu – I hope you like it.

  5. Killjoy
    said on April 23, 2010 at 8:06 am

    Jim: I used 15 sec min and 600 sec max. That stopped about 80% Then I later started to encrypt the time stamp I reached 100%

  6. Kevin
    said on April 25, 2010 at 11:55 am

    Hi Jim, just discovered you and your site, very interesting & inspiring.

    Yes it’s a simple idea. But – just as simple for the spammers to jump over. They just need to increase the time period before they send the spamm .

    Of course it’ll work though for a while, and I guess it’s worth it for that. The spammers are always trying to be one step ahead of us.

  7. Jim Westergren
    said on April 28, 2010 at 10:18 am

    Killjoy,

    Jim: I used 15 sec min and 600 sec max. That stopped about 80% Then I later started to encrypt the time stamp I reached 100%

    How did you encrypt it? Feel welcome to e-mail me.

    Hi Kevin,

    Yes it’s a simple idea. But – just as simple for the spammers to jump over. They just need to increase the time period before they send the spamm.

    It’s not that easy …

    If a spammer sends a spam to 1 million blogs and has to wait 10 seconds for each one then it would take 115 days for 1 spam comment …

  8. David
    said on May 1, 2010 at 5:17 pm

    Sorry to tell the “bad” news but this idea is not new. For example there are plugins for vBulletin that does this and xRumer probably beats it already too.

  9. Ostnan Mk
    said on May 2, 2010 at 7:55 am

    Hi,Jim
    I use the proxy to your Twitter.
    ShangHai World Expo yesterday, and you have plans to Shanghai?
    Your Motherland is Swedish right? I want to see your country venues.
    This is my N.NU’s Blog:www.ostnan.n.nu

  10. Jim Westergren
    said on May 3, 2010 at 8:51 am

    Hi David,
    Thanks for that info – then I learned something new. I have been running the above code for a while now and it blocks around 90% of the spam which is quite good but not good enough. Later when I have the time I will make some adjustments.

    Hi Ostnan,
    Yes, I live in Sweden at the moment and sorry no I don’t have any plans on coming to Shanghai. Good luck with your N.nu site and just ask if you get any questions.

  11. Ostnan Mk
    said on May 3, 2010 at 7:06 pm

    Hi!Jim
    What a pity! You don’t this plan.I think we have a dream around the world, although it isn‘t practical. But people still yearn.
    World Expo is a great platform, It helps me understand some of the country inaccessible.
    It is very attractive.
    If you have the opportunity I will go to Sweden, just let me fascinated by her name.
    If you plan to come to China, be sure to tell me, I think I would be a good guide.
    I have been here for a night, perhaps you there will be a sunny weather.Good night or good luck day?

  12. David
    said on May 7, 2010 at 7:46 am

    Yea, I’m seeing lots of innovation in this area. One idea I like also is having a separate url that is just for comments, keeping the main url completely free of external links. Calculated Risk Blog does this.

  13. Jim Westergren
    said on May 7, 2010 at 9:17 am

    Is this David as in “dvduval” – Administrator of phpLD?
    Nice to hear from you – I have been using your script since 2005. Thanks for making it or help to make it.

    Yes, having it on another URL is one way – but ugly in my opinion.

    Smashingmagazine.com are using another approach – not displaying any author URL.

  14. esoftload
    said on May 15, 2010 at 7:52 am

    it’s new for me. thanx for sharing….

  15. David
    said on May 18, 2010 at 7:17 pm

    Yes, that’s me. :)

    I also really like the Disqus addon. It not only helps protect from spam (I think everything is in javascript). It also makes it easy to track conversations across multiple blogs.

  16. Gwendolyn
    said on May 19, 2010 at 5:30 am

    Finally! An even handed and informative discourse on this interesting and diverse subject. Here’s hoping the author re-visits the issue with more information in the future

  17. Jim Westergren
    said on May 19, 2010 at 9:17 am

    @David,

    Yes, I am actually seriously considering to implement Disqus on this blog. I am running it as a comment system for the websites on http://www.N.nu and it is great.

  18. Aleks
    said on May 20, 2010 at 5:40 pm

    Web spam is web evil. All people must fight with it.
    Not bad information. Great.

  19. Aleks
    said on May 20, 2010 at 5:42 pm

    It is very difficult to fight with spam. We can help on the site
    Web studio MicrowebNET

  20. Gloria
    said on May 27, 2010 at 9:36 am

    I’m re-thinking what I thought I knew before reading this. Thanks for a well written piece that breaks through the fog to present a clear picture on a fascinating subject.

  21. karan
    said on June 1, 2010 at 5:44 am

    great info tutorial,super thanks a lot

  22. florida web design
    said on June 9, 2010 at 4:13 am

    Nicely done. I’ve had to integrate CAPTCHA’s in to my clients’ sites and it stops 100% of the spam.

  23. Jack harris
    said on June 9, 2010 at 4:57 pm

    Nice blog, Glad to see this!SEO Ranking Solutions is renowned as the leader in the scenario of search engine optimisation by providing excellent, cost effective services. SEO is a highly competitive marketing tool requiring intense focus and intact knowledge in the working of search engine algorithms and robots. Internet is used as the optimal device to search for diverse products and services. Millions of searches are performed to find out particular information.

  24. Learn Programming
    said on June 13, 2010 at 1:11 am

    When you can stop spam with captcha but there are some tools which recognize then captcha and attacks you.

  25. Oleg
    said on June 16, 2010 at 9:07 pm

    Hi Jim! Just discovered your method and i like it very much. I have some idea too. I use it with forms in my sites with user-generated content and it protects them well. I call it multilayered captcha, look for details here http://makebusiness.ru/idea/49 (russian lang., but there is working example+google translate will help). It also have some potential to extend..

  26. MultiSearch
    said on June 17, 2010 at 10:03 am

    Well my personal opinion is that no matter what we do, we cant totally stop it. There are various captcha verification software and session creating apps which can fail our checks.

  27. Ira Mann
    said on June 24, 2010 at 5:46 pm

    Very interesting idea on how to block spam. Sometimes the easiest solutions are the simplest one (I think that is a quote from the movie “Contact”). Anyway, nice post as usual.

    Thanks

  28. K Jones
    said on July 12, 2010 at 9:32 pm

    Hi, I clicked your blog link from phplinkdirectory signature. I really love this trick you have here. And it is easy to understand and should work on those autoposters. I friggen hate the world of spam. Are you signing up with that new project honeypot cloud thing?

  29. bpo projects
    said on July 31, 2010 at 9:05 am

    Yes i appreciate your words,its really very great one this one very helpful blog for the business men.the i’ll absolutely get lot’s of information regarding seo process that how it works.

  30. Kingston Upon Thames
    said on August 4, 2010 at 9:16 pm

    I amazed how Matt Cutts handle all the spam team of Google. Really its one of the main tough task to maintain all the spam. I appreciate your knowledge,its really very great one this one very helpful blog for the business men. Wonderful post, I really enjoyed reading it! The people you meet when you travel really are often the best part of it all!

  31. Bogdan
    said on August 9, 2010 at 9:19 am

    Jim, this is an awesome idea, will definitely use it for some of my blogs!

  32. Jim Westergren
    said on August 11, 2010 at 7:08 pm

    I just updated this article with some stats.

  33. caily
    said on August 25, 2010 at 11:12 am

    Web spam is really annoying which troubles me a lot.Thank you for the article I will test the sugestions you provide.

Leave a Comment

To quote: <blockquote>text to quote</blockquote>

You can use these tags: <a href=""> <blockquote> <code> <em> <strong>


To have a photo of you beside your comments, upload a photo here.

Previous post in category:

Next post in category:

Design by myself:  valid XHTML and valid CSS.