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']&& 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.             echo 'Error initializing CAPTCHA!';
  65.             exit;
  66.         }
  67.     
  68.         // Get CAPTCHA secret passphrase
  69.         $_SESSION['phrase'$c->getPhrase();
  70.     
  71.         // Get CAPTCHA image (as PNG)
  72.         $png $c->getCAPTCHA();
  73.         if (PEAR::isError($png)) {
  74.             echo 'Error generating CAPTCHA (maybe font missing?)!';
  75.             exit;
  76.         }
  77.         file_put_contents(md5(session_id()) '.png'$png);
  78.     
  79.         echo '<form method="post">' 
  80.              '<img src="' md5(session_id()) '.png?' time('" />' 
  81.              '<input type="text" name="phrase" />' .
  82.              '<input type="submit" /></form>';
  83.     }
  84. ?>

Documentation generated on Mon, 11 Mar 2019 14:44:40 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.