Scripts
Somcaptcha
Om spam te vermijden heb ik geprobeerd om 2 methodes te combineren. Een captcha-afbeelding met daarin een som die opgelost moet worden. Graag hoor ik of deze captcha sterk genoeg zou zijn en waar ik mijn script nog kan verbeteren. Getest op php5, waarschijnlijk werkt het ook op php4. (De font kan je hier downloaden: http://zamna.be/captcha/fonts/GeosansLight.ttf)
somcaptcha
*** afbeelding.php ***
<?php
session_start();
header("Content-type: image/gif");
header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
header("Last-Modified: " . date("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
//create image
$width = strlen($_SESSION['numberwork']) * 10;
$height = 18;
$image = imagecreate($width, $height);
//set white as transparant color
$backgroundcolor = imagecolorallocatealpha($image, 215, 215, 215, 127);
imagecolortransparent($image, $backgroundcolor);
//noise
for($i = 0; $i < 8; $i++) {
$noiseColor = imagecolorallocate($image, rand(205, 255), rand(205, 255), rand(205, 255));
$noiseX1 = rand(1, $width/2);
$noiseY1 = rand(1, $height);
$noiseX2 = rand($width/2, $width);
$noiseY2 = rand(1, $height);
imageline($image, $noiseX1, $noiseY1, $noiseX2, $noiseY2, $noiseColor);
$text = rand(1,9);
$fontsize = 12;
$grey = rand(220, 255);
$fontcolor = ImageColorAllocate($image, $grey, $grey, $grey);
$font = 'fonts/GeosansLight.ttf';
$angle = rand(-20,20);
$x = rand(0, $width);
$y = rand(0, $height);
imagettftext($image, $fontsize, $angle, $x, $y, $fontcolor, $font, $text);
}
//add text
$text = $_SESSION['numberwork'];
for($i = 0; $i <= strlen($text); $i++) {
$character = substr($text, $i, 1);
$fontsize = rand(12,16);
$fontcolor = ImageColorAllocate($image, rand(0,100), rand(0,100), rand(0,100));
$font = 'fonts/GeosansLight.ttf';
$angle = rand(-10,10);
if ($character == 'i' || $character == 'l') {
$x = $i * 10.5;
} else {
$x = $i * 10;
}
$y = 14;
imagettftext($image, $fontsize, $angle, $x, $y, $fontcolor, $font, $character);
}
//clean up
imagegif($image);
imagedestroy($image);
?>
*** voorbeeld.php ***
<?php
session_start();
if (isset($_POST['loginsum']) && $_POST['loginsum'] != "" && $_POST['loginsum'] == $_SESSION['loginsum']) {
echo 'Correcte som';
}
if (isset($_POST['loginsum']) && $_POST['loginsum'] != "" && $_POST['loginsum'] != $_SESSION['loginsum']) {
echo 'Foute som';
}
//numbers
$_NUMBERS = array();
$_NUMBERS[0] = "nul";
$_NUMBERS[1] = "een";
$_NUMBERS[2] = "twee";
$_NUMBERS[3] = "drie";
$_NUMBERS[4] = "vier";
$_NUMBERS[5] = "vijf";
$_NUMBERS[6] = "zes";
$_NUMBERS[7] = "zeven";
$_NUMBERS[8] = "acht";
$_NUMBERS[9] = "negen";
//calculations
$_CALCULATIONS = array();
$_CALCULATIONS[0] = "plus";
$_CALCULATIONS[1] = "min";
//generate random sum
$loginnumber1 = rand(0,9);
$loginnumber2 = rand(0,9);
$calculation = rand(0,1);
$_SESSION['numberwork'] = $_NUMBERS[$loginnumber1]." ".$_CALCULATIONS[$calculation]." ".$_NUMBERS[$loginnumber2];
if ($calculation == 0) {
$sum = ($loginnumber1 + $loginnumber2);
} else {
$sum = ($loginnumber1 - $loginnumber2);
}
$_SESSION['loginsum'] = $sum;
?>
<form id="captcha_test" method="post" action="" onsubmit="">
<img src="afbeelding.php"> = <input name="loginsum" type="text" size="3" maxlength="3" /><br />
<input type="submit" value="test" />
</form>
Reacties
0