Hallo,
Ich versuche ein Captcha-Script zum Laufen zu bringen. Das Captcha-Bild wird erfolgreich erstellt, jedoch fällt auch der richtige Code beim Testen jedesmal durch. And der md5 cryptation kann es auch nicht liegen, da es auch ohne diese nicht klappt. Also kann es ja fast nur an der Session liegen?
So wird das Captcha Bild generiert:
Und so in das Formular gesetzt:
Und so teste ich die Eingabe des Benutzers:
Ich versuche ein Captcha-Script zum Laufen zu bringen. Das Captcha-Bild wird erfolgreich erstellt, jedoch fällt auch der richtige Code beim Testen jedesmal durch. And der md5 cryptation kann es auch nicht liegen, da es auch ohne diese nicht klappt. Also kann es ja fast nur an der Session liegen?
So wird das Captcha Bild generiert:
PHP:
<?php
session_start();
// generating the captcha code
function random_string($len=5, $str="") {
for ($i=0; $i<$len; $i++) {
// generates a random number that will be the ASCII code of the character
// we only want numbers (ascii code from 48 to 57) and caps letters (65 to 90)
$ord = rand(48, 90);
if ((($ord >= 48) && ($ord <= 57)) || (($ord >= 65) && ($ord <= 90))) {
$str .= chr($ord);
} else {
$str .= random_string(1);
}
}
return $str;
}
$rand_str = random_string(5);
// writing the code into the session
$_SESSION['image_value'] = md5($rand_str);
// creating the background
$image = imagecreate(87, 30);
$bg_color = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 33, 33, 33);
for ($i=0; $i<12; $i++) {
$y = rand(0, 30);
imageline($image, 0, $y, 87, $y, $white);
}
// some general parameters
$size = 15;
$top = 5;
$left = 5;
$space = 16;
$font = "Arial.ttf";
// isolating the different digits of the code
$letters[0] = substr($rand_str, 0, 1);
$letters[1] = substr($rand_str, 1, 1);
$letters[2] = substr($rand_str, 2, 1);
$letters[3] = substr($rand_str, 3, 1);
$letters[4] = substr($rand_str, 4, 1);
// defining some general colors
$colors[0] = array(122,229,112);
$colors[1] = array(85,178,85);
$colors[2] = array(226,108,97);
$colors[3] = array(141,214,210);
$colors[4] = array(214,141,205);
$colors[5] = array(100,138,204);
// formatting the different digits of the code
for ($i=0; $i<5; $i++) {
$r_angle = rand(-20, 20);
$r_color = rand(0, 5);
$r_size = rand($size-($size*25/100), $size+($size*25/100));
$font_color = imagecolorallocate($image, $colors[$r_color][0],$colors[$r_color][1],$colors[$r_color][2]);
imagettftext($image, $r_size, $r_angle, $left, $size+$top, $font_color, $font, $letters[$i]);
$left += $space;
}
// sending the captcha image to the browswer
header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
?>
Code:
<?php session_start(); ?>
<li class="captcha">
<label for="captcha">Verification: <em><img src="images/required_star.gif" /></em></label>
<input id="captcha" class="captcha" type="text" name="captcha" />
<script language="javascript" type="text/javascript">
function reloadCaptcha() {
document.getElementById("image").src = document.getElementById("image").src;
}
</script>
<span class="image">
<iframe id="image" style="border:0;width:87px;height:30px" src="verify.php"></iframe>
<a onclick="javascript:reloadCaptcha()">reload image</a>
</span>
</li>
PHP:
if (isset($_GET['captcha'])) {
if (md5($_POST['captcha']) != $_SESSION['image_value']) {
$errors[] = "Enter the correct verification code.";
}
} else {
$errors[] = "There were some problems. Please review your information.";
}