Cristal
jeudi 26 octobre 2006 à 07:57
CODE
<?php
session_start();
header ("Content-type: image/png");
$image = imagecreate(80,20); //On créer une image de 100 px sur 25
$gris = imagecolorallocate($image,230,230,230); //gris
$noir = imagecolorallocate($image,0,0,0); //noir
$bleu = imagecolorallocate($image,0,0,255); //bleu
$bleuclair = imagecolorallocate($image, 156, 227, 254);
$rouge = imagecolorallocate($image,255,0,0); //rouge
$vert = imagecolorallocate($image,0,255,0); //vert
$blanc = imagecolorallocate($image, 255, 255, 255);
$jaune= imagecolorallocate($image, 255, 255, 0);
imagefill($image,0,0,$blanc);
$nbr = mt_rand(); //on génére un nombre
$code = substr($nbr,0,6); //On limite à 6 caractères
$_SESSION['code'] = $code;
$font = imageloadfont("8x13iso.gdf"); //on insere une police
$x = mt_rand(15,20);
$y = mt_rand(0,7);
imagestring($image,6,$x,$y,$code,$noir); //on écrit sur l'image
$pointb = 1;
while($pointb <= 50) // Boucle pour faire 50 point bleu clair
{
$x = mt_rand(0,100);
$y = mt_rand(0,25);
ImageSetPixel($image, $x, $y, $jaune);
$pointb++;
}
$pointn = 1;
while($pointn <= 50) // Boucle pour faire 50 point gris
{
$x = mt_rand(0,100);
$y = mt_rand(0,25);
ImageSetPixel($image, $x, $y, $bleu);
$pointn++;
}
$pointr = 1;
while($pointr <= 50) // Boucle pour faire 50 point vert
{
$x = mt_rand(0,100);
$y = mt_rand(0,25);
ImageSetPixel($image, $x, $y, $vert);
$pointr++;
}
$ligne = 1;
while($ligne <= 2) //Tracer 2 lignes
{
$x1 = mt_rand(0,100);
$y1 = mt_rand(0,25);
$x2 = mt_rand(0,100);
$y2 = mt_rand(0,25);
ImageLine($image, $x1, $y1, $x2, $y2, $rouge);
$ligne++;
}
$xt1 = mt_rand(0,150);
$yt1 = mt_rand(0,50);
$xt2 = mt_rand(0,150);
$yt2 = mt_rand(0,50);
$xt3 = mt_rand(0,150);
$yt3 = mt_rand(0,50);
imagepng($image); //on dessine l'image
?>
Code vraiment simple mais qui marche...
Tu mets en plus dans ton formulaire :CODE
<img src="image.php" alt="Image" /> <input name="code" type="text" size="25" onfocus="this.value=''" value="Tapez le code de sécurité" />
Et la vérification :CODE
$codeSession = $_SESSION['code'];
$codeFormulaire = trim($_POST['code']);
if($codeFormulaire == NULL) { haut(); echo "Vous n'avez pas tapé le code de sécurité !<br /><br /><a href=\"inscription.php\">Retour</a>"; bas(); exit(); }
if($codeFormulaire != $codeSession) { haut(); echo "Le code de sécurité est incorrect !<br /><br /><a href=\"inscription.php\">Retour</a>"; bas(); exit(); } else {$_SESSION['code'] = "";}
A adapté bien entendu avec ton système.
(Si tu veux la police :
http://www.archive-host.com/8x13iso.gdf) (C'est un vieux code

)