Vous êtes ici Codes PHP et MySQL > Formulaires > Un Captcha accessible à tous

 

Un Captcha accessible à tous

Voici donc une petite classe bien utile: elle génère une question et les champs de formulaires associés.
La classe affiche elle-même le formulaire, il suffit donc de l'instancier comme suit:
$test = &new Turing();
Les éléments de formulaire seront:
input name="turing" id="turing" value="46" type="text"
input name="turingS" id="turingS" value="d9d4f495e875a2e075a1a4a6e1b9770f" type="hidden"
Lors du traitement du formulaire, il suffit de vérifier que md5($_POST['turing']) == $_POST['turingS'];
 
Crazycat
Site de l'auteur voir
Code vu 5106 fois
Enregistré le 21 Mars 2008
  • Digg ce code sur digg.com
  • Bookmark ce code sur del.icio.us
  • Bookmark ce code sur Google
  • Bookmark ce code sur Yahoo
  • Ajoute Un Captcha accessible à tous
  • Partage ce code sur Facebook
 
 
 
 

Code Source


01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
208.
209.
210.
211.
212.
213.
214.
215.
216.
217.
218.
219.
220.
221.
222.
223.
224.
225.
226.
227.
228.
229.
230.
231.
232.
233.
234.
235.
236.
237.
238.
239.
240.
241.
242.
243.
 
<?php
/******************************************************************************/
/*                                                                            */
/*                       __        ____                                       */
/*                 ___  / /  ___  / __/__  __ _____________ ___               */
/*                / _ \/ _ \/ _ \_\ \/ _ \/ // / __/ __/ -_|_-<               */
/*               / .__/_//_/ .__/___/\___/\_,_/_/  \__/\__/___/               */
/*              /_/       /_/                                                 */
/*                                                                            */
/*                                                                            */
/******************************************************************************/
/*                                                                            */
/* Titre          : Un Captcha accessible à tous                              */
/*                                                                            */
/* URL            : http://www.phpsources.org/scripts361-PHP.htm              */
/* Auteur         : Crazycat                                                  */
/* Date édition   : 21 Mars 2008                                              */
/* Website auteur : http://www.c-p-f.org                                      */
/*                                                                            */
/******************************************************************************/

/**
 * Classe de génération de test de Turing
 * Cette classe permet de générer un test de Turing (ou Captcha)
 * accessible à tous
 * @author CrazyCat <crazycat@c-p-f.org>
 * @copyright 20071022
 * @version 1.0
 * @package Captcha
 *
 * @TODO Traduction des différents éléments pour avoir les 5 langues
 */

class Turing {

    /**
     * Tableau des chiffres en littéral (0 -> 50)
     * @var mixed
     */
    var $arrNumbers;
   

    /**
     * Tableau des positions en lettres
     * @var mixed
     */
    var $arrPos;

    /**
     * Tableau des phrases type
     * @var mixed
     */
    var $strSentences;
   
    /**
     * Type de réponses attendures
     * @var mixed
     */
    var $strTypes;

    /**
     * Langue utilisée par le test de Turing
     * @var string
     */
    var $lang 'fr';
   

    /**
     * Valeur maximale d'une opération
     * @var int
     */
    var $MAX 50;

    /**
     * Tableau question / réponse
     * @var mixed
     */
    var $arrTest;
   
    /**
     * Option de debug
     * @var boolean
     */
    var $Debug false;

    /**
     * Constructeur PHP5
     * @param string $lang
     */
    function __construct($lang='fr'$Debug=false) {
        $this->lang = (string) $lang;
        switch($lang) {
            case 'en':
                break;
            case 'de':
                break;
            case 'it':
                break;
            case 'es':
                break;
            default:
                $this->arrNumbers = array('zéro''un''deux''trois''quatre''cinq''six''sept''huit''neuf''dix''onze''douze''treize''quatorze''quinze''seize''dix-sept''dix-huit''dix-neuf''vingt''vingt et un''vingt-deux''vingt-trois''vingt-quatre''vingt-cinq''vingt-six''vingt-sept''vingt-huit''vingt-neuf''trente''trente et un''trente-deux''trente-trois''trente-quatre''trente-cinq''trente-six''trente-sept''trente-huit''trente-neuf''quarante''quarante et un''quarante-deux''quarante-trois''quarante-quatre''quarante-cinq''quarante-six''quarante-sept''quarante-huit''quarante-neuf''cinquante');
                $this->arrPos = array('premier''second''troisième''quatrième''cinquième''sixième''septième''huitième''neuvième''dixième''onzième''douzième''treizième''quatorzième''quinzième''seizième''dernier');
                $this->strSentences = array(
                    'Quel est le %s caractère de %s ? (%s)',
                    'Dans %s, quel caractère vient après %s ? (%s)',
                    'Combien font %s et %s ? (%s)',
                    'Combien font %s moins %s ? (%s)',
                    'Combien font %s fois %s ? (%s)'
                );
                $this->strTypes = array('lettre''chiffre''nombre');
        }
        $choice rand(,6);
        switch($choice) {
            case 1$this->mot(, true); break;
            case 2$this->addition(); break;
            case 3$this->soustraction(); break;
            case 4$this->multiplication(); break;
            case 5$this->mot(1true); break;
            case 6$this->mot(1false); break;
            default: $this->mot(, false); break;
        }
        $this->genHtml($Debug);
    }
   
    /**
     * Constructeur PHP4
     * @param string $lang
     */
    function Turing($lang='fr',$Debug=false) {
        $this->__construct($lang$Debug);
    }

    /**
     * Renvoit la valeur littérale d'un nombre
     * @param int $i
     * @return string
     */
    function numbers($i) {
        if (intval($i)>) {
            return $this->arrNumbers[$i];
        }
    }
    /**
     * Génère une question sur la position d'un caractère
     * @param integer $strType Jeu de caractères à utiliser
     * @param boolean $unique Chaine à caractères uniques ou non
     */
    function mot($strType$unique=false) {
        if ($strType==1$chars = array('0''1''2''3''4''5''6''7''8''9');
        else $chars = array('a''b''c''d''e''f''g''h''i''j''k''l''m''n''o''p''q''r''s''t''u''v''w''x''y''z');
        $arrChars = array();
        for ($i=;$i<50;$i++) {
            $arrChars[] = $chars[rand(, count($chars)-1)];
        }
        if ($unique===true) {
            $tmpChars array_values(array_unique($arrChars));
            $arrChars $tmpChars;
        }
        $strTuring implode(""$arrChars);
        if (strlen($strTuring)>17$strTuring substr($strTuring, , rand(8,17))
;
        if ($unique === true) {
            $chrSelected rand(1,strlen($strTuring)-1);
            $this->arrTest = array(sprintf($this->strSentences[1], $strTuring$arrChars[($chrSelected 1)], $this->strTypes[$strType]), $arrChars[
$chrSelected]);
        } else {
            $chrSelected rand(,strlen($strTuring) -);
            if ($chrSelected == (strlen($strTuring) -1)) {
                $this->arrTest = array(sprintf($this->strSentences[], $this->
arrPos[16], $strTuring$this->strTypes[$strType]), $arrChars[$chrSelected]);
            } else {
                $this->arrTest = array(sprintf($this->strSentences[], $this->
arrPos[$chrSelected], $strTuring$this->strTypes[$strType]), $arrChars[
$chrSelected]);
            }
        }
    }

    /**
     * Génère une addition
     */
    function addition() {
        while (($c =($a rand(1$this->MAX)) + ($b rand(1$this->MAX))) > 
$this->MAX );
        $this->arrTest = array(sprintf($this->strSentences[2], $this->numbers($a
), $this->numbers($b), $this->strTypes[2]), $c);
    }

    /**
     * Génère une soustraction
     */
    function soustraction() {
        while ( ( $c =($a rand(1$this->MAX)) - ($b rand(1$this->MAX))) <
 );
        $this->arrTest = array(sprintf($this->strSentences[3], $this->numbers($a
), $this->numbers($b), $this->strTypes[2]), $c);
    }

    /**
     * Génère une multiplication
     */
    function multiplication() {
        while ( ( $c =($a rand(1$this->MAX)) * ($b rand(1$this->MAX))) >
 $this->MAX );
        $this->arrTest = array(sprintf($this->strSentences[4], $this->numbers($a
), $this->numbers($b), $this->strTypes[2]), $c);
    }

    /**
     * Génération du formulaire html
     * Affiche 3 éléments de formulaire:
     * <label> est la question
     * <input turing> est le champ de réponse
     * <input tutingS> est la valeur codée (MD5) de la réponse
     * @return string Eléments du formulaire
     */
    function genHtml($Debug=false) {
        if ($Debug===truevar_dump($this->arrTest);
        $strForm '<label for="turing">'.$this->arrTest[].
'</label><input type="text" name="turing" id="turing" value="" /><input' .
' type="hidden" name="turingS" id="turingS" value="'.md5($this->arrTest[1]).
'" />';
        echo $strForm;
    }
}
?>

 

Fonctions du code

: Crée un tableau - (PHP 4, PHP 5)
: Génère une valeur aléatoire - (PHP 4, PHP 5)
: - (PHP 4, PHP 5)
: Compte le nombre d'éléments d'un tableau ou le nombre de propriétés...
: Dédoublonne un tableau - (PHP 4 >= 4.0.1, PHP 5)
: Retourne toutes les valeurs d'un tableau - (PHP 4, PHP 5)
: Rassemble les éléments d'un tableau en une chaîne - (PHP 4, PHP 5)
: Calcule la taille d'une chaîne - (PHP 4, PHP 5)
: Retourne un segment de chaîne - (PHP 4, PHP 5)
: Retourne une chaîne formatée - (PHP 4, PHP 5)
: Affiche les informations d'une variable - (PHP 4, PHP 5)
: Calcule le md5 d'une chaîne - (PHP 4, PHP 5, PECL hash:1.1-1.3)
: Affiche une chaîne de caractères - (PHP 4, PHP 5)
Ajouter un commentaire
Code de sécurité

Attention: Les champs marqués d'une étoile * sont obligatoires
 
Librairie PHP

Connexion

 
 

Formulaires

 
 

PHP

 
 
 
 
    Offres d'emploi

Plus de 500 offres d'emploi PHP/MySQL

Offres d'emploi
 
    Editeur PHP
 
        Publicité