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

Source for file qfcaptcha_form_random.php

Documentation is available at qfcaptcha_form_random.php

  1. <?php
  2.  
  3. /**
  4.  * HTML_QuickForm_CAPTCHA mix example - Form
  5.  *
  6.  * In this example, we'll pick the type of CAPTCHA loaded at runtime. So, if
  7.  * the answer doesn't match the first time, the user may be prompted for a
  8.  * different type of CAPTCHA on the next try.
  9.  *
  10.  * PHP versions 4 and 5
  11.  *
  12.  * @category   HTML
  13.  * @package    HTML_QuickForm_CAPTCHA
  14.  * @subpackage Examples
  15.  * @author     Philippe Jausions <Philippe.Jausions@11abacus.com>
  16.  * @copyright  2006-2008 by Philippe Jausions / 11abacus
  17.  * @license    http://www.opensource.org/licenses/bsd-license.php New BSD
  18.  * @version    CVS: $Id: qfcaptcha_form_random.php,v 1.1 2008/04/26 23:27:32 jausions Exp $
  19.  * @filesource
  20.  * @link       http://pear.php.net/package/HTML_QuickForm_CAPTCHA
  21.  * @see        qfcaptcha_image.php
  22.  */
  23.  
  24. /**
  25.  * Because the CAPTCHA elements are serialized in the PHP session,
  26.  * you need to include the class declarations BEFORE the session starts.
  27.  * So BEWARE if you have php.ini session.auto_start enabled, you won't be
  28.  * able to use this element, unless you're also using PHP 5's __autoload()
  29.  * or php.ini's unserialize_callback_func setting
  30.  */
  31. require_once 'HTML/QuickForm.php';
  32. require_once 'HTML/QuickForm/CAPTCHA/Equation.php';
  33. require_once 'HTML/QuickForm/CAPTCHA/Figlet.php';
  34. require_once 'HTML/QuickForm/CAPTCHA/Image.php';
  35. require_once 'HTML/QuickForm/CAPTCHA/Word.php';
  36.  
  37.  
  38. /**
  39.  * A session is required to store the Text_CAPTCHA instance
  40.  */
  41.  
  42. // Options are mixed between the different CAPTCHA drivers
  43. // (see the other examples for more details)
  44. $options = array('sessionVar'   => basename(__FILE__'.php'),
  45.  
  46.                  'options'      => array('font_file' => array(
  47.                                          '/usr/share/fonts/figlet/basic.flf',
  48.                                          '/usr/share/fonts/figlet/big.flf',
  49.                                          '/usr/share/fonts/figlet/nancyj.flf',
  50.                          )),
  51.                  'width'        => 250,
  52.                  'height'       => 90,
  53.  
  54.                  'callback'     => 'qfcaptcha_image.php?var='
  55.                                    .basename(__FILE__'.php'),
  56.                  'imageOptions' => array(
  57.                      'font_size' => 20,
  58.                      'font_path' => '/usr/share/fonts/truetype/',
  59.                      'font_file' => 'cour.ttf'),
  60.     );
  61.  
  62. $form = new HTML_QuickForm('qfCaptcha');
  63.  
  64. // Pick a random CAPTCHA driver
  65. $drivers = array('CAPTCHA_Word',
  66.                  'CAPTCHA_Image',
  67.                  'CAPTCHA_Equation',
  68.                  'CAPTCHA_Figlet',
  69.                 );
  70. $captcha_type $drivers[array_rand($drivers)];
  71.  
  72. // Create the CAPTCHA element:
  73. $captcha_question =$form->addElement($captcha_type'captcha_question',
  74.                                        'How do you understand this?'$options);
  75. if (PEAR::isError($captcha_question)) {
  76.     echo $captcha_type ' :: ' $captcha_question->getMessage();
  77.     exit;
  78. }
  79.  
  80. $captcha_answer =$form->addElement('text''captcha''Enter the answer');
  81.  
  82. $form->addRule('captcha''Enter your answer',
  83.                'required'null'client');
  84.  
  85. $form->addRule('captcha''What you entered didn\'t match. Try again.',
  86.                'CAPTCHA'$captcha_question);
  87.  
  88. $form->addElement('checkbox''blocker''Validate Form?',
  89.                   'Uncheck this box to fail form validation (beside CAPTCHA)');
  90. $form->addRule('blocker''Form failed validation''required');
  91.  
  92. $form->setDefaults(array('blocker' => 1));
  93.  
  94. $form->addElement('submit''''Verify');
  95.  
  96. ?>
  97. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  98.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  99. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  100. <head>
  101. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  102. <title>HTML_QuickForm_CAPTCHA random example</title>
  103. </head>
  104. <body>
  105. <h1>Random CAPTCHA Type HTML QuickForm</h1>
  106.  
  107. <?php
  108. if ($form->validate()) {
  109.     // Prevent the re-use of the same CAPTCHA phrase for future submissions
  110.     // (If you do not destroy the CAPTCHA, someone could reuse the same
  111.     // answer over and over again...)
  112.     $captcha_question->destroy();
  113.  
  114.     // Don't need to see CAPTCHA related elements
  115.     $form->removeElement('captcha_question');
  116.     $form->removeElement('captcha');
  117.     $form->freeze();
  118.     echo '<p>Value matched!</p>';
  119.  
  120.     $form->display();
  121.  
  122. else {
  123.     // Since the CAPTCHA created within this call might be of a different type
  124.     // from one created from a previous call (i.e. page refresh, or form
  125.     // didn't validate) we need to scrap the old instance (of Text_CAPTCHA).
  126.     // That's what the destroy() method does.
  127.     // Downside is if the CAPTCHA was answered properly but the form
  128.     // otherwise didn't pass validation, the user will have to reenter a new
  129.     // answer to the CAPTCHA...
  130.     $captcha_question->destroy();
  131.     $captcha_answer->setValue('');
  132.  
  133.     $form->display();
  134. }
  135.  
  136. ?>
  137. </body>
  138. </html>
  139. <?php
  140.  
  141. highlight_file(__FILE__);
  142.  
  143. ?>

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