|
Moderator
Status: Offline
Beiträge: 735
Registriert seit: 15.01.2005
Ort: Oberbuchsiten
Alter: 36
|

13.12.2006, 18:22
Hier also mal meine Captcha-Lösung. Wer sich mit PHP auskennt, findet vielleicht auch noch Verbesserungen, aber es funktioniert zumindest:
captcha.php:[php]<?php
function createCaptcha($width, $height)
{
mt_srand((double)microtime()*1000000);
$captcha = mt_rand(1000, 99999);
$captcha = md5($captcha);
$captcha = substr($captcha, 0, 5);
$captcha = strtoupper($captcha);
$_SESSION['captcha'] = $captcha;
$image = imagecreate($width, $height) or die("Can't initialize GD image stream");
$bg_color = imagecolorallocate($image, 255, 255, 255);
$line_color = imagecolorallocate($image, 0, 0, 0);
$elpise_color = imagecolorallocate($image, 200, 200, 200);
$elpise_color2 = imagecolorallocate($image, 235, 235, 235);
$text_color = imagecolorallocate($image, 0, 0, 0);
$rand_h1 = mt_rand(1, 40);
$rand_h2 = mt_rand(1, 40);
$rand_h3 = mt_rand(1, 40);
$rand_h4 = mt_rand(1, 40);
$rand_h5 = mt_rand(1, 40);
$rand_v1 = mt_rand(1, 100);
$rand_v2 = mt_rand(1, 100);
$rand_v3 = mt_rand(1, 100);
$rand_v4 = mt_rand(1, 100);
$rand_v5 = mt_rand(1, 100);
imageline($image, 0, $rand_h1, 100, $rand_h1, $line_color);
imageline($image, 0, $rand_h2, 100, $rand_h2, $line_color);
imageline($image, 0, $rand_h3, 100, $rand_h3, $line_color);
imageline($image, 0, $rand_h4, 100, $rand_h4, $line_color);
imageline($image, 0, $rand_h5, 100, $rand_h5, $line_color);
imageline($image, $rand_v1, 0, $rand_v1, 50, $line_color);
imageline($image, $rand_v2, 0, $rand_v2, 50, $line_color);
imageline($image, $rand_v3, 0, $rand_v3, 50, $line_color);
imageline($image, $rand_v4, 0, $rand_v4, 50, $line_color);
imageline($image, $rand_v5, 0, $rand_v5, 50, $line_color);
imagefilledellipse($image, mt_rand(0, 100), mt_rand(0, 40), mt_rand(10, 40), mt_rand(10, 25), $elpise_color);
imagefilledellipse($image, mt_rand(0, 100), mt_rand(0, 40), mt_rand(20, 40), mt_rand(10, 25), $elpise_color);
imagefilledellipse($image, mt_rand(0, 100), mt_rand(0, 50), mt_rand(20, 50), mt_rand(10, 35), $elpise_color2);
if (function_exists("Imagettftext")) {
Imagettftext ($image, 22, 0, 10, 30, $text_color, "".realpath("font.ttf"), $captcha);
} else {
imagestring($image, 5, 25, 12, $captcha, $text_color);
}
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
}
session_start();
createCaptcha(100, 40);
?>
[/php]
Eingebunden wird das ganze dann einfach mit <img src="captcha.php">, was bereits das Bild anzeigt und den Prüfcode in die Session schreibt. Also braucht man nur noch den eingegebenen Code aus dem Formular mit demjenigen in der Session zu vergleichen und entsprechende Schritte einleiten. Damit das Captcha wirklich vernünftig funktioniert, braucht es natürlich noch eine entsprechend "krakelige" Schrift als "font.ttf". Live sieht man das wie schon gesagt u. a. auf meiner HP im Gästebuch.
Dieses Posting wurde aus 100% rezyklierten Elektronen hergestellt
und kann für die Umwelt absolut unschädlich gelöscht werden.
|