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.         );
  48.  
  49.         // Set CAPTCHA options
  50.         $options = array(
  51.             'width' => 200,
  52.             'height' => 80,
  53.             'output' => 'png',
  54.             'imageOptions' => $imageOptions
  55.         );
  56.                    
  57.         // Generate a new Text_CAPTCHA object, Image driver
  58.         $c Text_CAPTCHA::factory('Image');
  59.         $retval $c->init($options);
  60.         if (PEAR::isError($retval)) {
  61.             echo 'Error initializing CAPTCHA!';
  62.             exit;
  63.         }
  64.     
  65.         // Get CAPTCHA secret passphrase
  66.         $_SESSION['phrase'$c->getPhrase();
  67.     
  68.         // Get CAPTCHA image (as PNG)
  69.         $png $c->getCAPTCHA();
  70.         if (PEAR::isError($png)) {
  71.             echo 'Error generating CAPTCHA (maybe font missing?)!';
  72.             exit;
  73.         }
  74.         file_put_contents(md5(session_id()) '.png'$png);
  75.     
  76.         echo '<form method="post">' 
  77.              '<img src="' md5(session_id()) '.png?' time('" />' 
  78.              '<input type="text" name="phrase" />' .
  79.              '<input type="submit" /></form>';
  80.     }
  81. ?>

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