captcha-spam-protection

Gesponsorde koppelingen

PHP script bestanden

  1. captcha-spam-protection

« Lees de omschrijving en reacties

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
<?php
#######################################
## © 2008 Wouter De Schuyter (Paradox)
## <[email protected]>
## http://paradox-productions.net/
## CAPTCHA V1.0 (SPAM PROTECTION)
#######################################


session_start(); // START SESSION

// FUNCTION TO SELECT A RANDOM CHARACTER OUT OF A STRING

function random_char($string) {
    $length = strlen($string);
    $position = mt_rand(0, $length - 1);
    return $string[$position];
}


$width = 75; // IMG WIDTH (PX)
$height = 25; // IMG HEIGHT (PX)
$characters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789"; // CHARACTERS FOR CAPTCHA STRING
$font = "fonts/font.ttf"; // FONT LOCATION
$fontS = 12; // FONT SIZE (PX)
$min = 50; // MIN NUMBER FOR THE RANDOM RGB TEXT COLOR
$max = 200; // MAX NUMBER FOR THE RANDOM RGB TEXT COLOR

// ADVANCED
////////////

$positionCharacterX = 3; // POSITION CHARACTER 1 ON THE X-AXIS (PX)
$characterSpace = 20; // SPACE FOR 1 CHARACTER (PX)
$positionCharactersY = 17; // SPACE ON THE Y-AXIS (PX)

$img = imagecreate($width, $height);
imagecolorallocate($img, 255, 255, 255); // BACKGROUND COLOR IN RGB

$randNr1 = rand($min, $max); // RANDOM NUMBER 1 BETWEEN $min & $max
$randNr2 = rand($min, $max); // RANDOM NUMBER 2 BETWEEN $min & $max
$randNr3 = rand($min, $max); // RANDOM NUMBER 3 BETWEEN $min & $max

$randomChar1 = random_char($characters); // RANDOM CHARACTER 1
$randomChar2 = random_char($characters); // RANDOM CHARACTER 2
$randomChar3 = random_char($characters); // RANDOM CHARACTER 3
$randomChar4 = random_char($characters); // RANDOM CHARACTER 4

$textcolor1 = imagecolorallocate($img, $randNr1, $randNr2, $randNr3); // TEXT COLOR 1
$textcolor2 = imagecolorallocate($img, $randNr2, $randNr3, $randNr1); // TEXT COLOR 2
$textcolor3 = imagecolorallocate($img, $randNr3, $randNr1, $randNr2); // TEXT COLOR 3
$textcolor4 = imagecolorallocate($img, $randNr3, $randNr2, $randNr1); // TEXT COLOR 4

imagettftext($img, $fontS, 0, $positionCharacterX + 0 * $characterSpace, $positionCharactersY, $textcolor1, $font, $randomChar1); // CHARACTER 1
imagettftext($img, $fontS, 0, $positionCharacterX + 1 * $characterSpace, $positionCharactersY, $textcolor2, $font, $randomChar2); // CHARACTER 2
imagettftext($img, $fontS, 0, $positionCharacterX + 2 * $characterSpace, $positionCharactersY, $textcolor3, $font, $randomChar3); // CHARACTER 3
imagettftext($img, $fontS, 0, $positionCharacterX + 3 * $characterSpace, $positionCharactersY, $textcolor4, $font, $randomChar4); // CHARACTER 4

$_SESSION['captcha'] = $randomChar1 . $randomChar2 . $randomChar3 . $randomChar4; // CAPTCHA STRING FOR SESSION

header("content-type: image/png"); // CONTENT TYPE

imagepng($img); // CREATE IMAGE
imagedestroy($img); // DESTROY IMAGE
?>


Je kan het controleren d.m.v. deze code:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
<?php
    if($_POST['CaptchaVerefication'] !== $_SESSION['captcha']) {
        echo "<p>The captcha vereficatie was fout! Probeer a.u.b. opnieuw.</p>";
        $error == true; // (DIT GEBRUIK IK OM MN FORMS TE CONTROLEREN OP HET EINDE => OPTIONEEL)
    }
?>



Note: Ik weet dat de vermenigvuldiging op lijn 49 & 50 vrij nutteloos is maar heb het gedaan omdat ik het zelf overzichtlijker vind.

 
 

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.