--- date: "2006-03-04T04:00:46Z" title: Using Akismet in PHP4 to Defeat Comment Spam --- <p>I've been having comment spam problems on <a href="http://paulduncan.org/" title="All my non-tech-related posts.">my personal page</a>. <a href="http://wordpress.org/" title="Wordpress, the popular PHP-based blogging tool.">Wordpress</a> uses <a href="http://akismet.com/" title="Anti-spam system used by Wordpress.">Akismet</a> for comment spam filtering now-a-days, and there are <a href="http://akismet.com/development/" title="Akismet bindings for several popular programming languages.">bindings</a> for several languages, including <a href="http://php.net/" title="Version 4 of the PHP programming language."><acronym title="PHP: Hypertext Preprocessor">PHP</acronym>5</a>, <a href="http://kemayo.wordpress.com/2005/12/02/akismet-py/" title="Akismet bindings for Python.">Python</a>, and <a href="http://www.blojsom.com/blog/nerdery/2005/12/02/Akismet-API-in-Ruby.html" title="Akismet bindings for Ruby.">Ruby</a>. The <a href="http://akismet.com/development/api/" title="Akismet API documentation.">Akismet <acronym title="Application Programming Interface">API</acronym> documentation</a> documentation has a <a href="http://php.net/" title="Version 4 of the PHP programming language."><acronym title="PHP: Hypertext Preprocessor">PHP</acronym>4</a>-friendly code snippet, but a quick <a href="http://google.com/" title="The infamouse Google search engine.">Google</a> search didn't turn up any full-blown <a href="http://php.net/" title="Version 4 of the PHP programming language."><acronym title="PHP: Hypertext Preprocessor">PHP</acronym>4</a> bindings, so I wrote my own. Here's an example of the <acronym title="Application Programming Interface">API</acronym>:</p> <pre><code># # Check comment using Akismet (http://akismet.com/). Returns true for # spam, and false for ham. # function is_comment_spam($news_id, $name, $email, $url, $comment) { global $AKISMET_CONFIG; # populate comment information $comment_data = array( 'user_ip' => $_SERVER['REMOTE_ADDR'], 'user_agent' => $_SERVER['HTTP_USER_AGENT'], 'referrer' => $_REQUEST['REFERER'], 'permalink' => "http://paulduncan.org/?id=$news_id", 'comment_type' => 'comment', 'comment_author' => $name, 'comment_author_email' => $email, 'comment_author_url' => $url, 'comment_content' => $comment, ); # create akismet handle $ak = new Akismet($AKISMET_CONFIG['api_key'], $AKISMET_CONFIG['blog']); # return akismet result (true for spam, false for ham) return $ak->check_comment($comment_data); } </code></pre> <p><a href="http://pablotron.org/files/akismet-php4-0.1.0.tar.gz" title="Download a tarball of my PHP4 Akismet bindings.">Download php4-akismet-0.1.tar.gz</a> (<a href="http://pablotron.org/files/akismet-php4-0.1.0.tar.gz.asc" title="OpenPGP signature for version 0.1.0 of my PHP4 Akismet bindings.">Signature</a>)</p> <p><b>Update:</b> Fixed a minor typo in the example.</p>