aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2006-03-04-using-akismet-in-php4-to-defeat-comment-spam.html
blob: 6de39cb961e3cda347e009393b6977faa8853dfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
---
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'               =&gt; $_SERVER['REMOTE_ADDR'],
    'user_agent'            =&gt; $_SERVER['HTTP_USER_AGENT'],
    'referrer'              =&gt; $_REQUEST['REFERER'],
    'permalink'             =&gt; "http://paulduncan.org/?id=$news_id",
    'comment_type'          =&gt; 'comment',
    'comment_author'        =&gt; $name,
    'comment_author_email'  =&gt; $email,
    'comment_author_url'    =&gt; $url,
    'comment_content'       =&gt; $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-&gt;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>