Search:
Main Menu
Login | RSS |

Quick PHP Code Tips and Examples

PHP Programming Tips, Tutorials and Source Code Examples for newbie

Integrate Akismet anti-spam service in PHP Scripts

March 26th, 2007 by Jon Moffet

Everybody knows, spam is an annoying thing in the internet. Especially when you create a Contact Form or a Guestbook only to have it filled with junk messages.

Luckily the folks at Automattic have created Akismet as a measures to combat web spams, and best of all it can be integrated right inside your own customized PHP scripts.

First of all, you need to download Akismet PHP4 Class library (and it works with PHP5 too!)

Then get the Wordpress API key from Wordpress.com, simply sign up for an account there, and the API key can be obtained from "My Account" link at the top right when you log in.

Then include akismet.php class file in your php file that does form processing.

PHP:
  1. require('akismet.class.php');
  2.  
  3. $wpkey = '**YOUR WORDPRESS KEY***';
  4.  
  5. $comment = array(
  6.         'author' => $comment_author,
  7.         'email' => $comment_author_email,
  8.         'website' => $comment_author_website,
  9.         'body' => $comment,
  10.         'permalink' => $comment_guestbook_url,
  11.         'user_ip' => $comment_author_ip,
  12.         'user_agent' => $comment_author_user_agent,
  13.         );
  14.        
  15.     // instantiate an instance of the class
  16.     $akismet = new Akismet('http://fakapster.com/', $wpkey, $comment);
  17.    
  18.  
  19.     if($akismet->isSpam()){
  20.            // store the comment but mark it as spam (in case of a mis-diagnosis)
  21.                //display a warning
  22.  
  23.              die('your comment is marked as spam');
  24.  
  25.  
  26.       
  27.     } else {
  28.          //this comment is not a spam
  29.  
  30.          //insert comment into database code
  31.  
  32.         }

Your guestbook (or online form) should be fairly resistant to spam attacks if you implement anti-spam measures using this method.

Tags: , , , , , , , ,

Bookmark Post:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • NewsVine
  • Reddit
  • Netvouz
  • Spurl
  • Furl
  • digg
  • YahooMyWeb
  • del.icio.us

Posted in Uncategorized |

Related Posts

7 Responses

  1. Quick PHP Code Tips » Blog Archive » Upgraded to Wordpress 2.1.3 and Spam Karma 2.3 Says:

    [...] is easy to be included in your own made php scripts as an anti spam solution. Please refer to Integrate Akismet anti-spam service in PHP Scripts, for a brief guide to do [...]

  2. Spam Protected Contact Form example (using Akismet) » Quick PHP Code Tips and Examples Says:

    [...] previously previously featured here and here . I’ve improved the contact form example to include Akismet spam protection in order to make it more resistant to spam [...]

  3. John Says:

    wZGYsk bhsdrjkvbfjksbhfsvkbdhfkv

  4. TestName Says:

    Test myfunction comment

  5. Ryan Says:

    Just to add a note that akismet is free under certain conditions and not under others so check their website before you go an implement this one

  6. Jon Moffet Says:

    yes, true. there are no such thing as free lunch. In this case, you (or your clients) might have to pay akismet service if it met with certain conditions

  7. Anika Says:

    just have to lately. anything say don’t I ,

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.