Encore un captcha

Description

/*bonjour, voici un code source sur lequel je travaille pour apprendre le php*/
/*merci d'avance pour vos critiques, conseils etc...*/
/*qui me permettraient d'améliorer mon script*/
/*captcha v1.1 2014*/
/*testez le en ligne ici :*/
/*http://codingandscripting.comli.com/_code/*/


<?php
/*le code est mis a jour avec une variable de session, 
j ai donc supprimer le champ de texte invisible dans le formulaire*/

session_start();

$capt = "";

$a = 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');

if (!isset($_POST['Submit']) || isset($_POST['recapt'])) {
    $i = range(0, 26);
    $arr = range('A', 'Z');

    $y = 1;
    for ($y = 1; $y <= 1; $y++) {
        $x = mt_rand($i[1], 25);
        $capt = mt_rand($i[8], 26) . $arr[$x] . mt_rand($i[1], 26) . mt_rand($i[1], 26) . $arr[$x] . mt_rand($i[1], 26);
        $width = 200;
        $height = 100;
        $dossier = 'img/';
        $font = 'font/airstrea.ttf';
        $fichier = basename($dossier . 'captcha_bck.jpg');
        $image_dir = 'img/ico/';
        $miniature = $image_dir . $fichier;
        $im = ImageCreateTrueColor($width, $height)
                or die("Erreur pour créer l'image");
        $blanc = ImageColorAllocate($im, 255, 255, 255);
        $gris = ImageColorAllocate($im, 50, 50, 50);
        $source = ImageCreateFromJpeg($dossier . $fichier);
        ImageCopyResampled($im, $source, 0, 0, 0, 0, $width, $height, $width, $height);

        /* ajout depuis classphp */
        $nbCircle = rand(7, 10);
        $b = rand(0, 255);
        $colorCircle = imagecolorallocate($im, $blanc, $blanc, $blanc);
        for ($i = 0; $i < $nbCircle; $i++)
            imageellipse($im, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $colorCircle);
        imagettftext($im, 20, 17, 47, 67, $gris, $font, $capt);
        imagettftext($im, 20, 20, 50, 70, $blanc, $font, $capt);

        //ajout ligne
        $nbLine = rand(5, 8);
        for ($i = 0; $i < $nbLine; $i++) {
            $r = rand(0, 255);
            srand();
            $v = rand(0, 255);
            srand();
            $b = rand(0, 255);
            $color = imagecolorallocate($im, $r, $v, $b);
            $x1 = rand(0, $width / 4);
            srand();
            $x2 = rand($width / 4, $width);
            $y1 = rand(0, $height / 4);
            srand();
            $y2 = rand($height / 4, $height);
            imageline($im, $x1, $y1, $x2, $y2, $color);
        }

        /*
         * fin ajout* 
         */


        // sauvegarde du résultat dans une session

        $_SESSION['captcha'] = $capt;
        
        //fin sauvegarde



        ImageJpeg($im, $miniature);
        echo '<div><img src="' . $miniature . '"></div>';
    }
}
?>

<!doctype html>
<html>
    <head>


        <meta charset="utf-8">
        <title>Document sans titre</title>
    </head>

    <body onload="javascript:">
        <form method="post" enctype="multipart/form-data" name="form1" id="form1" action="verify.php"">
            <input name="captcha" type="text" id="captcha" value="" >
            <input type="submit" name="Submit" id="submit" value="Envoyer">

        </form>
        <form method="post" name="form1" id="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>"><input type="submit" name="recapt"  value="recaptcha">
        </form>
    </body>
</html>



<?php
/******************page verif()*********************/
session_start();
if ($_POST['captcha'] == $_SESSION['captcha']) {
    echo 'access granted';
} else {
    echo 'access refused' . '<br>';
    echo '<a href="index.php">retour a l\'acceuil</a>';
}

?>

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.