Zend_Captcha-Operation.xml 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.captcha.operation">
  4. <title>Captcha Operation</title>
  5. <para>
  6. All <acronym>CAPTCHA</acronym> adapter implement
  7. <classname>Zend_Captcha_Adapter</classname>, which looks like the following:
  8. </para>
  9. <programlisting language="php"><![CDATA[
  10. interface Zend_Captcha_Adapter extends Zend_Validate_Interface
  11. {
  12. public function generate();
  13. public function render(Zend_View $view, $element = null);
  14. public function setName($name);
  15. public function getName();
  16. public function getDecorator();
  17. // Additionally, to satisfy Zend_Validate_Interface:
  18. public function isValid($value);
  19. public function getMessages();
  20. public function getErrors();
  21. }
  22. ]]></programlisting>
  23. <para>
  24. The name setter and getter are used to specify and retrieve the
  25. <acronym>CAPTCHA</acronym> identifier. <methodname>getDecorator()</methodname> can be used
  26. to specify a <classname>Zend_Form</classname> decorator either by name or returning an
  27. actual decorator object. The most interesting methods are
  28. <methodname>generate()</methodname> and <methodname>render()</methodname>.
  29. <methodname>generate()</methodname> is used to create the <acronym>CAPTCHA</acronym>
  30. token. This process typically will store the token in the session so that you may compare
  31. against it in subsequent requests. <methodname>render()</methodname> is used to render the
  32. information that represents the <acronym>CAPTCHA</acronym>, be it an image, a figlet, a
  33. logic problem, or some other <acronym>CAPTCHA</acronym>.
  34. </para>
  35. <para>
  36. A typical use case might look like the following:
  37. </para>
  38. <programlisting language="php"><![CDATA[
  39. // Creating a Zend_View instance
  40. $view = new Zend_View();
  41. // Originating request:
  42. $captcha = new Zend_Captcha_Figlet(array(
  43. 'name' => 'foo',
  44. 'wordLen' => 6,
  45. 'timeout' => 300,
  46. ));
  47. $id = $captcha->generate();
  48. echo "<form method=\"post\" action=\"\">";
  49. echo $captcha->render($view);
  50. echo "</form>";
  51. // On subsequent request:
  52. // Assume captcha setup as before, the value of $_POST['foo']
  53. // would be key/value array: id => captcha ID, input => captcha value
  54. if ($captcha->isValid($_POST['foo'], $_POST)) {
  55. // Validated!
  56. }
  57. ]]></programlisting>
  58. </sect1>
  59. <!--
  60. vim:se ts=4 sw=4 et:
  61. -->