Zend Form Element Captcha

The Zend framework includes CAPTCHA as a form element making it extremely easy to incorporate into a zend form. Here is some code to get you started:

Click here to view my blog about using ReCaptcha with Zend Form.

1. Zend form component (Registration.php)

class Form_Registration extends Zend_Form
//put your code here
public function init()
$this->setAttrib('id', 'formid');
$name = $this->createElement('text', 'name');
$name->setLabel('Enter Name');

$captcha = $this->createElement('captcha', 'captcha',
array('required' => true,
'captcha' => array('captcha' => 'Image',
'font' => 'C:/Windows/Fonts/arial.ttf',
'fontSize' => '24',
'wordLen' => 5,
'height' => '50',
'width' => '150',
'imgDir' => APPLICATION_PATH.'/../public/captcha',
'imgUrl' => Zend_Controller_Front::getInstance()->getBaseUrl().
'dotNoiseLevel' => 50,
'lineNoiseLevel' => 5)));

$captcha->setLabel('Please type the words shown:');


$submit = $this->createElement('submit', 'submit');


The captcha element is initialized as a Zend Form element (using the createElement() method). In this example, ‘Image captcha’ is used. The image captcha requires various config parameters that are passed as an array (shown above).

The ‘imgDir’ parameter contains the location where the captcha images will be generated and stored (as .png files). Note that on a Unix/Linux system, the web server must be granted write permissions to this folder.

The ‘imgUrl’ parameter is used by the framework to display the generated captcha on the screen (using the <img src..> tag)

2. The indexController (indexController.php) and indexAction:

class IndexController extends Zend_Controller_Action
public function indexAction()
// action body
$this->view->form = new Form_Registration();
if ($this->_request->isPost())
if ($this->view->form->isValid($_POST))
echo "Valid!";

Simple enough.. Initialize the registration form that was setup in step1 and display on the view. In case the form is submitted, use the isValid() call to determine if all form validations succeed. The captcha element is also automatically validated against the user input by this call.

3. The view script – Index.phtml

echo $this->form;

If all went well, when you run your project, you should see an output that looks like :



2 thoughts on “Zend Form Element Captcha

  1. Nice and easy captcha. I had problem of removing old captcha images from captcha directory so i added private function for GC from Zend Captcha Image class:

    protected $_suffix = ‘.png’;
    protected $_expiration = 30;
    protected $_imgDir = ‘/../public/captcha’;
    protected $_gcFreq = 10;

    //call this where you create captcha
    if (mt_rand(1, $this->_gcFreq) == 1)

    //and private function inside where you create captcha element
    private function _gc() {
    $expire = time() – $this->_expiration;
    $imgdir = APPLICATION_PATH . $this->_imgDir;
    if(!$imgdir || strlen($imgdir) _suffix);
    foreach (new DirectoryIterator($imgdir) as $file) {
    if (!$file->isDot() && !$file->isDir()) {
    if ($file->getMTime() _suffix
    if (substr($file->getFilename(), -($suffixLength)) == $this->_suffix) {

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s