Introduction | Zend_Captcha Programmer's Reference Guide | CAPTCHA Adapters

# Captcha Operation

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

1. interface Zend_Captcha_Adapter extends Zend_Validate_Interface
2. {
3. public function generate();
5. public function render(Zend_View $view, $element = null);
7. public function setName($name);
9. public function getName();
11. public function getDecorator();
13. // Additionally, to satisfy Zend_Validate_Interface:
14. public function isValid($value);
16. public function getMessages();
18. public function getErrors();
19. }

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:

1. // Creating a Zend_View instance
2. $view = new Zend_View();
4. // Originating request:
5. $captcha = new Zend_Captcha_Figlet(array(
6. 'name' => 'foo',
7. 'wordLen' => 6,
8. 'timeout' => 300,
9. ));
11. $id = $captcha->generate();
12. echo "<form method=\"post\" action=\"\">";
13. echo $captcha->render($view);
14. echo "</form>";
16. // On subsequent request:
17. // Assume captcha setup as before, the value of $_POST['foo']
18. // would be key/value array: id => captcha ID, input => captcha value
19. if ($captcha->isValid($_POST['foo'], $_POST)) {
20. // Validated!
21. }

Introduction | Zend_Captcha Programmer's Reference Guide | CAPTCHA Adapters