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$flags = 0{
  4.         if (!($file fopen($filename($flags 1'a' '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.  
  15. $ok = false;
  16.  
  17. $msg 'Please enter the text in the image in the field below!';
  18.  
  19. if ($_SERVER['REQUEST_METHOD'== 'POST'{
  20.  
  21.     if (isset($_POST['phrase']&& isset($_SESSION['phrase']&&
  22.         strlen($_POST['phrase']> 0 && strlen($_SESSION['phrase']> 0 &&
  23.         $_POST['phrase'== $_SESSION['phrase']{
  24.         $msg 'OK!';
  25.         $ok = true;
  26.     else {
  27.         $msg 'Please try again!';
  28.     }
  29.  
  30.     unlink(session_id('.png');
  31.  
  32. }
  33.  
  34. print "<p>$msg</p>";
  35.  
  36. if (!$ok{
  37.     
  38.     require_once 'Text/CAPTCHA.php';
  39.  
  40.     // Set CAPTCHA options (font must exist!)
  41.     $options = array(
  42.         'font_size' => 24,
  43.         'font_path' => './',
  44.         'font_file' => 'COUR.TTF'
  45.     );
  46.                    
  47.     // Generate a new Text_CAPTCHA object, Image driver
  48.     $c Text_CAPTCHA::factory('Image');
  49.     $retval $c->init(20080null$options);
  50.     if (PEAR::isError($retval)) {
  51.         echo 'Error generating CAPTCHA!';
  52.         exit;
  53.     }
  54.     
  55.     // Get CAPTCHA secret passphrase
  56.     $_SESSION["phrase"$c->getPhrase();
  57.     
  58.     // Get CAPTCHA image (as PNG)
  59.     $png $c->getCAPTCHAAsPNG();
  60.     if (PEAR::isError($png)) {
  61.         echo 'Error generating CAPTCHA!';
  62.         exit;
  63.     }
  64.     file_put_contents(session_id(".png"$png);
  65.     
  66.     echo '<form method="post">' 
  67.          '<img src="' session_id('.png?' time('" />' 
  68.          '<input type="text" name="phrase" />' .
  69.          '<input type="submit" /></form>';
  70. }
  71. ?>

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