Manuel Pear

Text_CAPTCHA

Implémentation des CAPTCHA (Completely Automated Public Turing tests to tell Computers and Humans Apart).

Introduction

Ce paquet fourni les fonctionnalités pour créer un CAPTCHAs (Completely Automated Public Turing tests to tell Computers and Humans Apart). Les fonctionnalités proposées sont:

The package creates CAPTCHAs; due to the stateless nature of the HTTP protocol, the logic to secure a webpage using the package must be specifically implemented. Regardez l'exemple d'utilisation pour des informations détaillées.

Pour le moment, le driver graphique CAPTCHA dépend de Image_Text qui lui même requiert le support TTF qui doit être compilé en dans le PHP.

Exemple

L'exemple qui suit implémente l'utilisation commune d'un CAPTCHA: Soumettre un formulaire de donnée qui ne sera pris en compte que si le CAPTCHA a correctement été résolu.

Exemple 64-1. Créer un CAPTCHA

Le code qui suit crée un CAPTCHA, provides the relevant information for the package, anhd retrieves the CAPTCHA as a PNG image.

  1. <?php
  2. require_once 'Text/CAPTCHA.php';  
  3.  
  4. // Fixons les options du CAPTCHA (la fonte doit exister!)
  5. $imageOptions = array( 
  6.    'font_size'      => 24, 
  7.    'font_path'      => './', 
  8.    'font_file'      => 'COUR.TTF', 
  9.    'text_color'   => '#DDFF99', 
  10.    'lines_color'   => '#CCEEDD', 
  11.    'background_color' => '#555555'  
  12. );  
  13.  
  14. $options = array( 
  15.    'width' => 200, 
  16.    'height' => 80, 
  17.    'output' => 'png', 
  18.    'imageOptions' => $imageOptions  
  19. );  
  20.  
  21. // Générer un nouvel objet Text_CAPTCHA, pour le pilote Image
  22. $c = Text_CAPTCHA::factory('Image');  
  23. $retval = $c->init($options);  
  24. if (PEAR::isError($retval)) { 
  25.    printf ('Erreur pendant la génération du CAPTCHA: %s', 
  26.          $retval->getMessage()); 
  27.    exit;  
  28. }  
  29.  
  30. // Récupérer la phrase secrete du CAPTCHA
  31. $_SESSION['phrase'] = $c->getPhrase();  
  32.  
  33. // Prendre l'image CAPTCHA (PNG)
  34. $png = $c->getCAPTCHAAsPNG();  
  35. if (PEAR::isError($png)) { 
  36.    echo 'Erreur pendant la génération du CAPTCHA!'; 
  37.    echo $retval->getMessage(); 
  38.    exit;  
  39. }  
  40. file_put_contents(md5(session_id()) . '.png', $png  
  41. ?> 

Exemple 64-2. Protéger un formulaire avec un CAPTCHA

Le code suivant implémente la fonctionnalité de vérification qu'un CAPTCHA a été correctement ou pas résolu. Pour cela, la phrase du CAPTCHA est stockée dans une variable de session pour conserver la l'information à comparer. Il est important de vider (unset) la session après résolution du CAPTCHA pour éviter une réutilisation de l'ID de session.

  1. <?php
  2. session_start();  
  3. $ok = false;  
  4. $msg = 'Veuillez introduire dans le champs ci dessous '  
  5. .   'le texte qui apparait dans l\'image!';  
  6. if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
  7.    if (isset($_POST['phrase']) && is_string($_POST['phrase']) && isset($_SESSION['phrase']) && 
  8.       strlen($_POST['phrase']) > 0 && strlen($_SESSION['phrase']) > 0 && 
  9.       $_POST['phrase'] == $_SESSION['phrase']) { 
  10.       $msg = 'OK!'; 
  11.       $ok = true; 
  12.       unset($_SESSION['phrase']); 
  13.    } else { 
  14.       $msg = 'Veuillez ré-essayer!'; 
  15.    } 
  16.    unlink(md5(session_id()) . '.png');  
  17. }  
  18. print "<p>$msg</p>";  
  19.  
  20. if (!$ok) { 
  21.    // Crée le CAPTCHA comme vu ci-dessus
  22.    // et l'envoyer au client
  23. ?> 

Regardez le fichier CAPTCHA_test.php dans le paquet distribué pour un exemple complet et fonctionnel (support GD et TTF requis).

Information about the CAPTCHA and inner workings

Text_CAPTCHA provides information about the CAPTCHA and makes its inner workings accessible using several functions defined in the base class; the specific implementation of them is up to the driver.

  • init() is used to initialize the CAPTCHA and provide further information, like Image_Text parameters.

  • _createCAPTCHA() generates the CAPTCHA, but is called internally by init().

    getPhrase() returns the CAPTCHA's phrase.

  • _createPhrase() creates a phrase for the CAPTCHA, but is used internally.

  • getCAPTCHA() returns the CAPTCHA, the format used depends on the driver implementation.


Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Bretagne - php - Moto - Kit graphique