Archives du mot-clef captcha

PHP Captcha

Derrière ce nom imprononçable ce cache un truc relativement simple. Afin de lutter contre les inscriptions massive de bot et autre joyeuseté webispherique, la majorité des gros sites utilisent ce système (google/yahoo/etc…)

Qu’est ce qu’un captcha pere castor ? Définition sur wikipedia

Donc voila un captcha developpé par moi même

captcha.php

1
2
3
4
5
6
7
8
9
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
class captcha {
 
    public function init() {
        $alpha = 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','1','2','3','4','5','6','7','8','9','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');
        shuffle($alpha);
        $captcha = '';
        for ($j = '0';$j < 7;$j++) {
            $i = rand(0,(count($alpha)-1));
            $captcha .= $alpha[$i];
        }
        $_SESSION['captcha'] = strtoupper($captcha);
        $width = 200;
        $height = 50;
        $image = imagecreatetruecolor($width,$height);
        $background_color = imagecolorallocate($image, rand(240, 255), rand(240, 255), rand(240, 255));
        imagefilledrectangle($image, 0, 0, $width, $height, $background_color);
        $color1 = imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
        $font = imageloadfont('fonts/anonymous.gdf');
        $color2 = imagecolorallocate($image, rand(0, 150), rand(0, 150), rand(0, 150));
        imagestring ($image, $font, 20, 4, $captcha, $color2);
        for($j = '0';$j <= rand(4000, 5000);$j++)
        {
            imagesetpixel($image,rand(1, $width-1),rand(1, $height-1),$color1);
        }
        for($i = 0;$i < $width;$i = $i + 10)
        {
            imageline($image,$i,0,$i,$height,$color1);
        }
        for($i = 0;$i < $height;$i = $i + 10)
        {
            imageline($image,0,$i,$width,$i,$color1);
        }
        header('Content-Type: image/png');
        imagepng($image);
        imagedestroy($image);
    }
}

Utilisation

$captcha = new captcha();
$captcha->init();

Pour l’afficher

<img src="captcha.php" alt="captcha"/>

Et comme tjs le captcha en action sur Quote de Porc

La police utilisé pour le captcha (anonymous.gdf)