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 options (font must exist!)
  43.         $options = array(
  44.             'font_size' => 24,
  45.             'font_path' => './',
  46.             'font_file' => 'COUR.TTF'
  47.         );
  48.                    
  49.         // Generate a new Text_CAPTCHA object, Image driver
  50.         $c Text_CAPTCHA::factory('Image');
  51.         $retval $c->init(20080null$options);
  52.         if (PEAR::isError($retval)) {
  53.             echo 'Error generating CAPTCHA!';
  54.             exit;
  55.         }
  56.     
  57.         // Get CAPTCHA secret passphrase
  58.         $_SESSION['phrase'$c->getPhrase();
  59.     
  60.         // Get CAPTCHA image (as PNG)
  61.         $png $c->getCAPTCHAAsPNG();
  62.         if (PEAR::isError($png)) {
  63.             echo 'Error generating CAPTCHA!';
  64.             exit;
  65.         }
  66.         file_put_contents(md5(session_id()) '.png'$png);
  67.     
  68.         echo '<form method="post">' 
  69.              '<img src="' md5(session_id()) '.png?' time('" />' 
  70.              '<input type="text" name="phrase" />' .
  71.              '<input type="submit" /></form>';
  72.     }
  73. ?>

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