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

Captcha Operation

All CAPTCHA adapter implement Zend_Captcha_Adapter, which looks like the following:

<?php
interface Zend_Captcha_Adapter extends Zend_Validate_Interface
{
    public function 
generate();

    public function 
render(Zend_View $view$element null);

    public function 
setName($name);

    public function 
getName();

    public function 
getDecorator();

    
// Additionally, to satisfy Zend_Validate_Interface:
    
public function isValid($value);

    public function 
getMessages();

    public function 
getErrors();
}

The name setter and getter are used to specify and retrieve the CAPTCHA identifier. getDecorator() can be used to specify a Zend_Form decorator either by name or returning an actual decorator object. The most interesting methods are generate() and render(). generate() is used to create the CAPTCHA token. This process typically will store the token in the session so that you may compare against it in subsequent requests. render() is used to render the information that represents the CAPTCHA, be it an image, a figlet, a logic problem, or some other CAPTCHA.

A typical use case might look like the following:

<?php
// Creating a Zend_View instance
$view = new Zend_View();

// Originating request:
$captcha = new Zend_Captcha_Figlet(array(
    
'name' => 'foo',
    
'wordLen' => 6,
    
'timeout' => 300,
));

$id $captcha->generate();
echo 
"<form method=\"post\" action=\"\">";
echo 
$captcha->render($view);
echo 
"</form>";

// On subsequent request:
// Assume captcha setup as before, the value of $_POST['foo']
// would be key/value array: id => captcha ID, input => captcha value
if ($captcha->isValid($_POST['foo'], $_POST)) {
    
// Validated!
}

Zend Framework