Akismet Class/Library for Codeigniter
Published: February 5th, 2009 by: Andrew
Using Akismet to block spam on your Wordpress blog is a no-brainer, but what about your custom blog or article site? It is possible to extend the Akismet anti-spam service to your own custom website. In this article, you will find a fully functional Akismet library for CodeIgniter as well as instructions and usage examples.
Setup & Configuration
Extract the archive into your CI application. There are two files – the library (class file) goes into the system/application/libraries directory, and the config file goes into the system/application/config directory. Then, be sure to set the values in the config file.
Usage Example
$this->akismet->user_ip = $_SERVER['REMOTE_ADDR']; $this->akismet->user_agent = $_SERVER['HTTP_USER_AGENT']; $this->akismet->referrer = $_SERVER['HTTP_REFERRER']; $this->akismet->permalink = 'http://example.com/articles/some-article'; $this->akismet->comment_type = 'comment'; $this->akismet->comment_author = 'viagra-test-123'; $this->akismet->comment_author_url = ''; $this->akismet->comment_author_email = 'user@example.com'; $this->akismet->comment_content = 'This is a test comment.'; $result = $this->akismet->is_spam($debug);
The example is pretty self-explanatory. Just fill in those necessary values and get a result from the is_spam() function. This function will return ‘true’ if the comment is found to be spam, or ‘false’ if it is ham. Be advised, that it may also return error information if the request is not valid, so in a production environment, test for spam with something like this:
$spam = ($this->akismet->is_spam($debug) == 'true') ? 1 : 0;
The above statement forces the $spam variable to be ‘1’ if spam, or ‘0’ is ham, or if there was an error making the call to Akismet. This sets it up to inserting into a MySQL boolean field.
That $debug variable is being passed by reference and is optional. It will be set to the response headers received by the Akismet servers, and you can see the contents in case something goes wrong.
Help Correct Akismet if they Mess Up
Their spam detection isn’t always correct, but the cool thing about this service is that you can help them correct their mistakes. (How cool is that?) If they made a mistake, use the following methods to submit the correct information:
/* set the class variables - same as the usage example above */ /* submit spam to Akismet that was flagged as ham */ $result = $this->akismet->is_spam($debug); /* submit ham to Akismet that was flagged as spam */ $result = $this->akismet->is_ham($debug);
Getting an API Key
Akismet only gives free API keys to WordPress blogs, so if you have one, just use that API key for your CI application. See this page for more information on API keys and Akismet for commercial use.
So there you have it – easy spam protection on any CodeIgniter website. This library can easily be extended to a non-CodeIgniter website. The only CI resource this library uses is the config feature.
If you find a bug or have another feature idea to add, please post a comment. 🙂
Feb 9th, 2010
2:12 pm
[…] Akismet Class/Library for Codeigniter – Don’t like spam? Of course. So get akismet rid your spam. […]
David68
Jan 2nd, 2011
3:22 am
Extract the archive into your CI application. There are two files