captcha-class

Gesponsorde koppelingen

PHP script bestanden

  1. captcha-class

« Lees de omschrijving en reacties

class.captcha.php:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
error_reporting(E-ALL);
session_start();

////////////////////////////////////////////////////////////////////////////////////////////
// $capcha = new captcha();                                                               //
// $captcha->make_captcha($lengte, $type, $kleur_patroon);                                //
//                                                                                        //
// $lengte bevat de waarde van het gewenst aantal karakters voor je captcha               //
//        standaard staat deze op 5;                                                      //
//                                                                                        //
// $type bevat het type hoe je de captcha wil opbouwen (zie hier beneden)                 //
//           standaard staat deze op 5;                                                      //
//            - 0 : alles                                                                   //
//            - 1 : enkel Numeric                                                           //
//            - 2 : enkel Lowercase                                                         //
//            - 3 : enkel Uppercase                                                         //
//            - 4 : Numeric/Lowercase                                                       //
//            - 5 : Numeric/Uppercase                                                       //
//            - 6 : Lowercase/Uppercase                                                     //
//                                                                                        //
// $kleur_patroon bevat het patroon en kan 0 of 1 zijn                                    //
//          standaard is deze 0 en is zwart de achtergrond met witte boord en witte letters //
//          standaard is deze 1 en is wit de achtergrond met zwarte boord en zwarte letters //
//                                                                                        //
//                                                                                        //
//    voorbeeld1: $captcha = new captcha();                                               //
//                $captcha->make_captcha();                                               //
//                                                                                        //
//    voorbeeld2: $captcha = new captcha();                                               //
//                $captcha->make_captcha(8);                                              //
//                                                                                        //
//    voorbeeld3: $captcha = new captcha();                                               //
//                $captcha->make_captcha(6, 1);                                           //
//                                                                                        //
//    voorbeeld4: $captcha = new captcha();                                               //
//                $captcha->make_captcha(5, 5, 1);                                        //
////////////////////////////////////////////////////////////////////////////////////////////


class captcha {

    // pas hier het path aan naar het path waar jouw fonts zich bevinden op de server
    var $fonts_path = '../../../resources/fonts/';
    
    // pas hier eventueel de lettertypes aan die je wenst te gebruiken
    // let er wel op dat je bij het kiezen rekening houd met de duidelijkheid!

    var $fonts = array(
            'arialbd.ttf',
            'calibrib.ttf',
            'comicbd.ttf',
            'courbd.ttf',
            'framdit.ttf',
            'georgiab.ttf',
            'impact.ttf',
            'seguibd.ttf',
            'tahomabd.ttf',
            'trebucbd.ttf',
            'verdanab.ttf'
    );
    // hieronder niets meer veranderen.
    
    // definiëren van de variabelen voor het verdere script

    var $chars;
    var
$type;
    var
$color;
    var
$char_array = array();
    var
$captcha_code;
    
    function
generate_code() {
        $this->make_char_array();
        // We maken een for loop om een random code te maken
        for($i = 0; $i < $this->chars; $i++) {
            // we pakken random getallen uit de array $this->char_array, -1 aangezien een array begint bij 0;
            $count = count($this->char_array);
            $rand_i = mt_rand(0, $count-1);
            // we stoppen de gegenereerde code in de variabele $this->captcha_code
            $this->captcha_code .= $this->char_array[$rand_i];
        }
    }
    function
make_char_array() {
        // aanmaken van de verschillende array's voor type keuze.
        // uit de array's zijn verwarrende karakters zoals 0 en O en i en 1 en G eruit gelaten
        // dit kan je evnetueel nog zelf wat aanpassen.

        $low_chars = array_merge(range('a', 'f'), array('h', 'j', 'k', 'm', 'n'), range('p', 'z'));
        $upp_chars = array_merge(range('A', 'F'), array('H', 'J', 'K', 'M', 'N'), range('P', 'Z'));
        $dec_chars = array_merge(range(2, 8), range(2, 8));
        
        // bepalen welk type er gekozen werd en de array met de correcte characters vullen.
        switch ($this->type) {
            case
0:
                $this->char_array = array_merge($low_chars, $upp_chars, $dec_chars, $dec_chars);
                break;
            case
1:
                $this->char_array = $dec_chars;
                break;
            case
2:
                $this->char_array = $low_chars;
                break;
            case
3:
                $this->char_array = $upp_chars;
                break;
            case
4:
                $this->char_array = array_merge($low_chars, $dec_chars);
                break;
            case
6:
                $this->char_array = array_merge($low_chars, $upp_chars);
                break;
            default:

                $this->char_array = array_merge($upp_chars, $dec_chars);
        }
    }
    function
make_captcha($chars=5, $type=5, $color=0) {
        $this->chars = $chars;
        $this->type = $type;
        $this->color = $color;
        $this->generate_code();
        
        // We laten het script weten dat we een plaatje gaan maken
        header('Content-type: image/png');    
                
        // we geven de breedte van de afbeelding op
        $img_width = $this->chars * 23;
        
        // we maken het plaatje aan
        $image = imagecreatetruecolor($img_width, 46);

        // we bepalen de kleuren die we nodig hebben        
        if ($this->color == 1) {
            $noise_colors = array(51, 102, 153, 204);
            $bg_color = imagecolorallocate($image, 255, 255, 255);
            $color = imagecolorallocate($image, 0, 0, 0);
        }
else {
            $noise_colors = array(51, 102, 153, 204);
            $bg_color = imagecolorallocate($image, 0, 0, 0);
            $color = imagecolorallocate($image, 255, 255, 255);
        }

        
        // achtergrond instellen en boorden aanmaken
        imagefill($image, 0, 0, $bg_color);
        imageline($image, 0, 0, 0, 46, $color); //een zwart lijntje links
        imageline($image, 1, 0, $img_width, 0, $color); //een zwart lijntje boven
        imageline($image, $img_width-1, 0, $img_width-1, 46, $color); //een zwart lijntje rechts
        imageline($image, 1, 45, $img_width-1, 45, $color); //en een zwart lijntje onder

        // random dots genereren op de achtergrond in random kleuren

        for( $i=0; $i<($img_width*44)/2; $i++ ) {
            $noise_color = imagecolorallocate($image, $noise_colors[array_rand($noise_colors)], $noise_colors[array_rand($noise_colors)], $noise_colors[array_rand($noise_colors)]);
            imagefilledellipse($image, mt_rand(2,$img_width-2), mt_rand(2,44), 1, 1, $noise_color);
        }

        // random lijnen genereren op de achtergrond in random kleuren
        for( $i=0; $i<($img_width*44)/100; $i++ ) {
            $noise_color = imagecolorallocate($image, $noise_colors[array_rand($noise_colors)], $noise_colors[array_rand($noise_colors)], $noise_colors[array_rand($noise_colors)]);
            imageline($image, mt_rand(2,$img_width-2), mt_rand(2,44), mt_rand(2,$img_width-2), mt_rand(2,44), $noise_color);
        }


        // we maken een for loop voor de letters
        for($i = 0; $i < $this->chars; $i++) {
            // we zetten de letters op de juiste plaats
            $font =    $this->fonts_path . $this->fonts[array_rand($this->fonts)];
            $pos_h = $i * 21 + 6;
            imagettftext($image,
                19,
                mt_rand(-20,20), //willekeurige rotatie
                $pos_h, //plaats horizontaal
                mt_rand(20,40), //random plaats verticaal
                $color, // voorkleur
                $font, //willekeurig lettertype
                $this->captcha_code[$i]); //met de letter
        }
        // We laten het script weten dat we een plaatje gaan maken
        header('Content-type: image/png');
        imagepng($image);
        imagedestroy($image);
        $_SESSION['captcha_code'] = sha1(md5($this->captcha_code));
    }    
}

?>


voorbeelden van gebruik: (andere voorbeelden in zipje te downloaden)

voorbeeld 1: (standaard gebruik zonder parameters)
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
<?php
include('class.captcha.php');

$captcha = new captcha();
$captcha->make_captcha();
?>


voorbeeld 2: (gebruik met alle parameters)
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
<?php
include('class.captcha.php');

$captcha = new captcha();
$captcha->make_captcha(8, 1, 1);
?>


voorbeeld van gebruik in formulier:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
39
40
41
42
43
44
45
46
47
48
49
<?
// We willen op de hoogte gehouden worden van alle errors / notices
ini_set('display_errors', 1);
error_reporting(E_ALL);

// we gaan met sessies werken
session_start();
?>

<html>
    <head>
        <title>Captcha Class</title>
    </head>
<body>

<?
// controlen of het formulier gesubmit is
if($_SERVER['REQUEST_METHOD'] == 'POST')
{

    if(!empty($_POST['captcha_code']) && sha1(md5($_POST['captcha_code'])) == $_SESSION['captcha_code'])
    {

        echo 'De code is correct, Gefeliciteerd!';
    }

    else {
        echo 'De ingevoerde code is niet juist!';
    }
}

else {
    $i = range(1, 8);
    echo '
        Je kan nu html plaatsen zonder probleem en pas verder op je pagina de captcha image gebruiken.
        <form method="post" action="'
.$_SERVER['PHP_SELF'].'">
        <p>
            <img src="c'
. $i[array_rand($i)] . '.php" />
        </p>
        <p>
            Neem de code precies zo over zoals aangegeven hierboven.<br />
            Hou dus rekening met Hoofdletter en Kleine letters.<br />
            <label for="captcha_code">Code:</label>
            <input type="text" name="captcha_code">
        </p>
        <p>
            <input type="submit" name="submit" value="Controleer">
        </p>
        </form>
    '
;
}

?>

</body>
</html>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.