Unusual FIGlet CAPTCHA in ZF2

In a web form, you sometimes need to use a CAPTCHA test. A CAPTCHA (stands for “Completely Automated Public Turing test to tell Computers and Humans Apart”) is a challenge-response test used in web sites for determining whether the user is a human or a robot.

A typical CAPTCHA test works using the following algorithm:

  1. Some secret sequence of characters (word) is generated server-side.
  2. The secret word is saved in a PHP session variable.
  3. The distorted image is generated based on the secret word.
  4. The image is then displayed on the web page to site user.
  5. The site user is asked to type characters shown on the image.
  6. If the characters typed by user are the same as the secret word
    saved in the session, the test is considered passed.

The CAPTCHA test allows to reliably distinguish humans from robots, because humans are easily able to recognise and reproduce characters from the distorted image, while robots are not (at the current stage of evolution of computer vision algorithms).

In Zend Framework 2, there are several types of CAPTCHA. The most widely used one requires that the user type the letters of a distorted image that is shown on the web page. But, in this post, I’d like to tell about an unusual CAPTCHA type called the Figlet CAPTCHA.

To add the Figlet CAPTCHA to your form model, call the form’s add()
method as follows:

namespace Application\Form;
// ...

class MyForm extends Form
    // ...    
    protected function addElements() {
        // ...         
        // Add the CAPTCHA field
          'type'  => 'captcha',
          'name' => 'captcha',
          'attributes' => array(                                                
          'options' => array(
            'label' => 'Human check',
            'captcha' => array(
                'class' => 'Figlet',
                'wordLen' => 6,
                'expiration' => 600,                     

Above, the captcha key of the configuration array (see line 19) contains the following
parameters for configuring the Figlet CAPTCHA algorithm attached to the form element:

  • the class parameter (line 20) should be either the full CAPTCHA adapter
    class name (\Zend\Captcha\Figlet) or its short alias (Figlet).

  • the wordLen parameter (line 21) defines the length of the secret word to be generated.

  • the expiration parameter (line 22) defines the CAPTCHA expiration period (in seconds).

To render the CAPTCHA field, add the following lines to your .phtml
view template file:

  <?php echo $this->formLabel($form->get('captcha')); ?>
  <?php echo $this->formElement($form->get('captcha')); ?>
  <?php echo $this->formElementErrors($form->get('captcha')); ?>

Now, open the web page in your web browser. Once that is done,
you should see a page like in the figure below.


Once the PHP renderer processes the view template, it generates HTML markup for
the CAPTCHA element like shown below:

  <label for="captcha">Human check</label>            
 __   _    __   __   _    _      ___     _    _    __   __  
| || | ||  \ \\/ // | \  / ||   / _ \\  | || | ||  \ \\/ // 
| '--' ||   \ ` //  |  \/  ||  | / \ || | || | ||   \ ` //  
| .--. ||    | ||   | .  . ||  | \_/ || | \\_/ ||    | ||   
|_|| |_||    |_||   |_|\/|_||   \___//   \____//     |_||   
`-`  `-`     `-`'   `-`  `-`    `---`     `---`      `-`'   
<input name="captcha[id]" type="hidden" value="b68b010eccc22e78969764461be62714">
<input name="captcha[input]" type="text">                              
<p class="help-block">Enter the letters above as you see them.</p>

Note: This post is an excerpt from Chapter 8 “Advanced Usage of Forms” of my book “Using Zend Framework 2“.


I am a software developer currently living in Tomsk, Russia. I received a PhD degree in Computer Science from Tomsk Polytechnic University in 2010. I have been professionally developing C/C++ and PHP software since 2005. I like contributing to open-source and writing programming articles for popular web resources, like CodeProject. Besides writing, I love skiing and watching Formula-1.

Tagged with: , , ,
Posted in Zend Framework 2

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

Follow ValuableThing on WordPress.com
%d bloggers like this: