Robert Basic's blog

Honeypot for Zend Framework

by Robert Basic on April 21, 2010.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

I just hacked up a little code snippet based on Matthew’s Honeypot Wordpress plugin. It’s basically just a Validator for a Zend Form element which is hidden from the user via CSS. Cause it’s hidden, users won’t see it, but spambots will, well, cause they are bots.

If the element is left empty, it’s valid, otherwise it’s not.

So, here’s the code:

class App_Validate_Honeypot extends Zend_Validate_Abstract
    const SPAM = 'spam';

    protected $_messageTemplates = array(
        self::SPAM => "I think you're a spambot. Sorry."

    public function isValid($value, $context=null)
        $value = (string)$value;

        if(is_string($value) and $value == ''){
            return true;

        return false;

I add the element to the form like this:

        'label' => 'Honeypot',
        'required' => false,
        'class' => 'honeypot',
        'decorators' => array('ViewHelper'),
        'validators' => array(
                'validator' => 'Honeypot'

There. Done.

Happy hackin’!

Tags: framework, honeypot, php, validator, zend, zf.
Categories: Development, Programming.

Thanks for reading! If you require help on a PHP project of any kind, let's talk!

Recent posts