HTML_QuickForm_CAPTCHA
[ class tree: HTML_QuickForm_CAPTCHA ] [ index: HTML_QuickForm_CAPTCHA ] [ all elements ]

Source for file qfcaptcha_form_equation.php

Documentation is available at qfcaptcha_form_equation.php

  1. <?php
  2.  
  3. /**
  4.  * HTML_QuickForm_CAPTCHA "Equation" example - Form
  5.  *
  6.  * PHP versions 4 and 5
  7.  *
  8.  * @category   HTML
  9.  * @package    HTML_QuickForm_CAPTCHA
  10.  * @subpackage Examples
  11.  * @author     Philippe Jausions <Philippe.Jausions@11abacus.com>
  12.  * @copyright  2006-2008 by Philippe Jausions / 11abacus
  13.  * @license    http://www.opensource.org/licenses/bsd-license.php New BSD
  14.  * @version    CVS: $Id: qfcaptcha_form_equation.php,v 1.1 2008/04/26 23:27:32 jausions Exp $
  15.  * @filesource
  16.  * @link       http://pear.php.net/package/HTML_QuickForm_CAPTCHA
  17.  */
  18.  
  19. /**
  20.  * Because the CAPTCHA element is serialized in the PHP session,
  21.  * you need to include the class declaration BEFORE the session starts.
  22.  * So BEWARE if you have php.ini session.auto_start enabled, you won't be
  23.  * able to use this element, unless you're also using PHP 5's __autoload()
  24.  * or php.ini's unserialize_callback_func setting
  25.  */
  26. require_once 'HTML/QuickForm.php';
  27. require_once 'HTML/QuickForm/CAPTCHA/Equation.php';
  28.  
  29. /**
  30.  * A session is required to store the Text_CAPTCHA instance
  31.  */
  32.  
  33. $options = array('sessionVar' => basename(__FILE__'.php'));
  34.  
  35. $form = new HTML_QuickForm('qfCaptcha');
  36.  
  37. $captcha_question =$form->addElement('CAPTCHA_Equation''captcha_question',
  38.                                        'Can you resolve this?'$options);
  39. if (PEAR::isError($captcha_question)) {
  40.     echo $captcha_question->getMessage();
  41.     exit;
  42. }
  43.  
  44. $captcha_answer =$form->addElement('text''captcha''Enter the answer');
  45.  
  46. $form->addRule('captcha''Enter the answer to the equation',
  47.                'required'null'client');
  48.  
  49. $form->addRule('captcha''What you entered didn\'t match the equation',
  50.                'CAPTCHA'$captcha_question);
  51.  
  52. $form->addElement('submit''''Verify');
  53.  
  54. ?>
  55. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  56.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  57. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  58. <head>
  59. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  60. <title>HTML_QuickForm_CAPTCHA Equation example</title>
  61. </head>
  62. <body>
  63. <h1>Equation CAPTCHA HTML QuickForm</h1>
  64. <?php
  65. if ($form->validate()) {
  66.     // Prevent the re-use of the same CAPTCHA phrase for future submissions
  67.     // (If you do not destroy the CAPTCHA, someone could reuse the same
  68.     // answer over and over again...)
  69.     $captcha_question->destroy();
  70.  
  71.     // Don't need to see CAPTCHA related elements
  72.     $form->removeElement('captcha_question');
  73.     $form->removeElement('captcha');
  74.     $form->freeze();
  75.     echo '<p>Value matched</p>';
  76.  
  77. else {
  78.     // Force a new CAPTCHA if the answer submitted was incorrect
  79.     // (this is not required, but it improves the effectiveness of the CAPTCHA
  80.     // by preventing brut force attack)
  81.     if ($form->getElementError('captcha')) {
  82.         $captcha_question->destroy();
  83.         $captcha_answer->setValue('');
  84.     }
  85.  
  86.     $form->display();
  87. }
  88.  
  89. ?>
  90. </body>
  91. </html>
  92. <?php
  93.  
  94. highlight_file(__FILE__);
  95.  
  96. ?>

Documentation generated on Mon, 11 Mar 2019 15:21:02 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.