Petit captcha avec contrôle JS

Répondre
Amery
Amery
Déconnecté
Salut,

voilà un petit bout de code pour ajouter un petit captcha à un formulaire sous forme d'une question "Combien font 4+5 ?".
le code md5.js téléchargeable à l'adresse : http://pajhome.org.uk/crypt/md5/md5.js
Un exemple : http://www.carougeinfo.ch/captcha/exemple


FICHIER checkform.js
  1.  
  2. function CheckForm()
  3. {
  4. var captcha=document.getElementById('form').captcha.value;
  5. var vcaptcha=document.getElementById('form').vcaptcha.value;
  6.  
  7. if(captcha == "") {
  8. alert('Veuillez répondre à la question anti-robot, svp.'); return false; }
  9. else if(hex_md5(captcha)!=vcaptcha) {
  10. alert('Votre réponse à la question anti-robot est incorrecte.'); return false; }
  11. return true;
  12. }
  13.  


Le formulaire :
  1.  
  2. <?php
  3. session_start();  
  4. if($_POST['captcha']!="" && $_POST['captcha']==$_SESSION['resultat'])  
  5. {  
  6. // Traitement des données du formulaire  
  7. }  
  8.  
  9. // Captcha  
  10. $nb1 = rand(1, 5);  
  11. $nb2 = rand(1, 5);  
  12. $somme = $nb1 + $nb2;  
  13. $_SESSION['resultat'] = $somme;  
  14. $captcha_crypted = md5($somme);  
  15.  
  16. ?>
  17.  
  18. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  19. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  20. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
  21. <head>
  22. <script type="text/javascript" src="checkform.js"></script>
  23. <script type="text/javascript" src="md5.js"></script>
  24. </head>
  25. <body>
  26.  
  27. <form id="form" onsubmit='return CheckForm()' action="" method='post'>
  28. <div>Combien font<? echo $nb1; ?>+<? echo $nb2; ?>?</div>
  29. <div><input class="input" type="text" size="20" name="captcha" /></div>
  30. <div><small><i>
  31. (Pour vérifier qu'il ne s'agit pas d'un robot qui saisit le formulaire)
  32. </i></small></div>
  33. <div><input class="input" type="submit" value="envoyer" /></div>
  34. <div>
  35. <input type="hidden" name="vcaptcha" value="<?php echo $captcha_crypted; ?>"/>
  36. </div>
  37. </form>
  38.  


Rien de bien sorcier, mais ça fonctionne bien.

Amery
Répondre
Accès rapide :

Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Breizh Blog