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

Source for file CAPTCHA_test.php

Documentation is available at CAPTCHA_test.php

  1. <?php
  2.     if (!function_exists('file_put_contents')) {
  3.         function file_put_contents($filename$content{
  4.             if (!($file fopen($filename'w'))) {
  5.                 return false;
  6.             }
  7.             $n fwrite($file$content);
  8.             fclose($file);
  9.             return $n $n : false;
  10.         }
  11.     }
  12.  
  13.     // Start PHP session support
  14.     session_start();
  15.  
  16.     $ok = false;
  17.  
  18.     $msg 'Please enter the text in the image in the field below!';
  19.  
  20.     if ($_SERVER['REQUEST_METHOD'== 'POST'{
  21.  
  22.         if (isset($_POST['phrase']&& is_string($_POST['phrase']&& isset($_SESSION['phrase']&&
  23.             strlen($_POST['phrase']> 0 && strlen($_SESSION['phrase']> 0 &&
  24.             $_POST['phrase'== $_SESSION['phrase']{
  25.             $msg 'OK!';
  26.             $ok = true;
  27.             unset($_SESSION['phrase']);
  28.         else {
  29.             $msg 'Please try again!';
  30.         }
  31.  
  32.         unlink(md5(session_id()) '.png');
  33.  
  34.     }
  35.  
  36.     print "<p>$msg</p>";
  37.  
  38.     if (!$ok{
  39.     
  40.         require_once 'Text/CAPTCHA.php';
  41.  
  42.         // Set CAPTCHA image options (font must exist!)
  43.         $imageOptions = array(
  44.             'font_size'        => 24,
  45.             'font_path'        => './',
  46.             'font_file'        => 'COUR.TTF',
  47.             'text_color'       => '#DDFF99',
  48.             'lines_color'      => '#CCEEDD',
  49.             'background_color' => '#555555'
  50.         );
  51.  
  52.         // Set CAPTCHA options
  53.         $options = array(
  54.             'width' => 200,
  55.             'height' => 80,
  56.             'output' => 'png',
  57.             'imageOptions' => $imageOptions
  58.         );
  59.                    
  60.         // Generate a new Text_CAPTCHA object, Image driver
  61.         $c Text_CAPTCHA::factory('Image');
  62.         $retval $c->init($options);
  63.         if (PEAR::isError($retval)) {
  64.             printf('Error initializing CAPTCHA: %s!',
  65.                 $retval->getMessage());
  66.             exit;
  67.         }
  68.     
  69.         // Get CAPTCHA secret passphrase
  70.         $_SESSION['phrase'$c->getPhrase();
  71.     
  72.         // Get CAPTCHA image (as PNG)
  73.         $png $c->getCAPTCHA();
  74.         if (PEAR::isError($png)) {
  75.             printf('Error generating CAPTCHA: %s!',
  76.                 $png->getMessage());
  77.             exit;
  78.         }
  79.         file_put_contents(md5(session_id()) '.png'$png);
  80.     
  81.         echo '<form method="post">' 
  82.              '<img src="' md5(session_id()) '.png?' time('" />' 
  83.              '<input type="text" name="phrase" />' .
  84.              '<input type="submit" /></form>';
  85.     }
  86. ?>

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