créer un captcha et l'insérer dans un formulaire

alfyheim
Invité n'ayant pas de compte PHPfrance

23 mai 2009, 13:22

bonjour, voila mon souci :

je viens de créer un formulaire avec des tests de champs prérequis et maintenant il ne me reste plus qu'a y mettre un captcha. Je sais faire un captcha de base(je veux dire par là
<?php
				$t1=rand(0, 10);
				$t2=rand(0, 10);
				$resultat=$t1+$t2;
				echo $t1.' + '.$t2;?>
si on peut appeler ça un captcha)
mais je souhaite vraiment miser sur une bonne sécurité. Or les captchas ce n'est pas ça qui manque sur la toile me direz vous cependant je suis un peux largué concernant son intégration.

Ma question connaissez vous un site proposant un bon tuto ( mais genre bien expliqué car parfois cela manque cruellement de pédagogie) ou pourriez vous via l'exemple si dessous me ficeler quand a l'intégration. Merci infiniment
 	


<?php
/******************************************************************************/
/*                                                                            */
/*                       __        ____                                       */
/*                 ___  / /  ___  / __/__  __ _____________ ___               */
/*                / _ \/ _ \/ _ \_\ \/ _ \/ // / __/ __/ -_|_-<               */
/*               / .__/_//_/ .__/___/\___/\_,_/_/  \__/\__/___/               */
/*              /_/       /_/                                                 */
/*                                                                            */
/*                                                                            */
/******************************************************************************/
/*                                                                            */
/* Titre          : Image anti spam V2                                        */
/*                                                                            */
/* URL            : http://www.phpsources.org/scripts300-PHP.htm              */
/* Auteur         : R@f                                                       */
/* Date édition   : 08 Sept 2007                                              */
/*                                                                            */
/******************************************************************************/
?>
 >> le contenu dans l'image sera dans $_SESSION['livreor']
 >> 4 caractères

 Si le script est enregistré dans le fichier: anti_spam.php
 on appèle l'image comme ceci:
 <img src="anti_spam.php?name=livreor&strlen=4" alt="anti-flood" />

 Si $spam représente l'entrée utilsateur, le test se fait comme ceci:
 if( $_SESSION['livreor'] != strtoupper( $spam ) )
// erreur ici


<?php
session_start();

// type de flood
$name = $_GET['name'];
// nb de caractères
$strlen = (int) $_GET['strlen'];

// taille de l'image ( width )
$width = $strlen * 23 + 20;
$height = 60;
// taille de chaque zone de couleur
$widthColor = $width / 4;

// création
$img = imagecreatetruecolor( $width, $height );
// antialising, c'est plus bo! :-)
imageantialias( $img, 1 );

// chaine
$string = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
$chaine = '';
for( $i = ; $i < $strlen; $i++ )
  $chaine .= $string[ mt_rand( , 35 ) ];
  
$_SESSION[ $name ] = $chaine;

// couleur de départ
$c1 = array( mt_rand( 200, 255), mt_rand( 200, 255), mt_rand( 200, 255) );
// couleur finale
$c2 = array( mt_rand( 70, 180), mt_rand( 70, 180), mt_rand( 70, 180) );
// pas pour chaque composante de couleur
$diffsColor = array( ( $c1[] - $c2[] ) / $widthColor, ( $c1[1] - $c2[1] ) / 
$widthColor, ( $c1[2] - $c2[2] ) / $widthColor );

$start = ;
$end = $widthColor;

for( $j = ; $j < 4; $j++ ) // boucle pour chacune des 4 zones
{
  $r = $j % 2 ==  ? $c1[] : $c2[]; // composante r de d?part
  $v = $j % 2 ==  ? $c1[1] : $c2[1]; // idem v
  $b = $j % 2 ==  ? $c1[2] : $c2[2]; // idem b
  
  // création des lignes
  for( $i = $start; $i < $end; $i++ )
  {
    if( $j % 2 ==  )
    {
      $r -= $diffsColor[];
      $v -= $diffsColor[1];
      $b -= $diffsColor[2];
    }
    else
    {
      $r += $diffsColor[];
      $v += $diffsColor[1];
      $b += $diffsColor[2];
    }
    
    $color = imagecolorallocate( $img, $r, $v, $b );
    
    imageline( $img, $i, , $i, $height, $color );
  }
  
  $start += $widthColor;
  $end += $widthColor;
}

$colorsChar = array( ); // on va mémoriser les couleurs des caractères

// caractères
for( $i = ; $i < $strlen; $i++ )
{
  $colorsChar[$i] = imagecolorallocate( $img, mt_rand( , 120 ), mt_rand( , 120 )
, mt_rand( , 120 ) );
  imagettftext( $img, mt_rand( 20, 25 ), mt_rand( -35, 35 ), 10 + $i * 23, 35, 
$colorsChar[$i], 'comic.ttf', $chaine[ $i ] );
}

// quelques lignes qui embetent
for( $i = ; $i < 10; $i++ )
{
  imageline( $img, mt_rand(, $width), mt_rand(, $height), mt_rand(, $width), 
mt_rand(, $height), $colorsChar[mt_rand( , $strlen - 1 )] );
}

$noir = imagecolorallocate( $img, , ,  );

// bordure
imageline( $img, , , $width, , $noir );
imageline( $img, , , , $height, $noir );
imageline( $img, $width - 1, , $width - 1, $height, $noir );

// header: image
header("Content-type: image/png");
imagepng( $img ); 
imagedestroy( $img );
?>


alfyheim
Invité n'ayant pas de compte PHPfrance

23 mai 2009, 18:48

personne pour me venir en aide ? En tant que débutant (a peine deux semaines de pratique)
j'assimile bien mais bon.

Avatar du membre
ViPHP
ViPHP | 3008 Messages

23 mai 2009, 22:53