PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Check for spam

Zend_Service_Akismet::isSpam($data) is used to determine if the data provided is considered spam by Akismet. It accepts an associative array as the sole argument. That array requires the following keys be set:

  • user_ip, the IP address of the user submitting the data (not your IP address, but that of a user on your site).

  • user_agent, the reported UserAgent string (browser and version) of the user submitting the data.

The following keys are also recognized specifically by the API:

  • blog, the fully qualified URL to the resource or application. If not specified, the URL provided to the constructor will be used.

  • referrer, the content of the HTTP_REFERER header at the time of submission. (Note spelling; it does not follow the header name.)

  • permalink, the permalink location, if any, of the entry the data was submitted to.

  • comment_type, the type of data provided. Values specified in the API include 'comment', 'trackback', 'pingback', and an empty string (''), but it may be any value.

  • comment_author, the name of the person submitting the data.

  • comment_author_email, the email of the person submitting the data.

  • comment_author_url, the URL or home page of the person submitting the data.

  • comment_content, the actual data content submitted.

You may also submit any other environmental variables you feel might be a factor in determining if data is spam. Akismet suggests the contents of the entire $_SERVER array.

The isSpam() method will return either TRUE or FALSE, or throw an exception if the API key is invalid.

Example 707. isSpam() Usage

<?php
$data 
= array(
    
'user_ip'              => '111.222.111.222',
    
'user_agent'           => 'Mozilla/5.0 ' . (WindowsUWindows NT ' .
                              '
5.2en-GBrv:1.8.1Gecko/20061010 ' .
                              '
Firefox/2.0',
    '
comment_type'         => 'contact',
    '
comment_author'       => 'John Doe',
    '
comment_author_email' => 'nospam@myhaus.net',
    '
comment_content'      => "I'm not a spammerhonest!"
);
if (
$akismet->isSpam($data)) {
    echo "
Sorrybut we think you're a spammer.";
} else {
    echo "Welcome to our site!";
}

isSpam() implements the comment-check Akismet API method.

Zend Framework