CAPTCHA
HejJeg har forsøgt at lave en captcha, men den har et problem med at headers allready sent, og viser kun en masse tegn og ikke et billede
Kan nogle af jer hjælpe med hvad der er galt.
Jeg skal helst have den liggende som en funktion
<?
function make_captcha($captcha_width, $captcha_heigth, $pass_phrase,$font_size){
// Opretter billede
$img = imagecreatetruecolor($captcha_width, $captcha_heigth);
// sætter farver
$bg_color = imagecolorallocate($img, 255, 255, 255);
$text_color = imagecolorallocate($img, 15, 15, 15);
$graphic_color = imagecolorallocate($img, 200, 200, 200);
// Laver baggrundsfarve
// imagefilledrectangle($img, 0, 0, $captcha_width, $captcha_heigth, $bg_color);
// Danner noget baggrundsstøj i lyse farver
for($i=0;$i<($captcha_width*$captcha_heigth);$i++) {
$color = imagecolorallocate($img, mt_rand(155, 255), mt_rand(155, 255), mt_rand(155, 255));
imagesetpixel($img, ($x - 1), $y, $color);
$x += 1;
if($x > $captcha_width) {
$y += 1;
$x = 1;
}
}
// Gør at den kan finde fonten
putenv('GDFONTPATH=' . realpath('.'));
// Beregner placering i højden
$middle1 = $captcha_heigth - $font_size;
$middle2 = $middle1 / 2;
// Laver teksten
imagettftext($img, $font_size, mt_rand(-2, 2), 5, $captcha_heigth - $middle2, $text_color, 'verdana.ttf', $pass_phrase);
// Danner nogle prikker
for($i=0; $i < 50; $i++) {
imagesetpixel($img, rand() % $captcha_width, rand() % $captcha_heigth, $graphic_color);
}
// Danner nogle streger
for ($i=0; $i < 3; $i++) {
imageline($img, 0, rand() % $captcha_heigth, $captcha_width, rand() % $captcha_heigth, $graphic_color);
}
// Sender billedet via header
header("Content-type; image/png");
imagepng($img);
// Rydder op
imagedestroy($img);
}
function generate_text($count) {
// Godkendte tegn
$allowed= array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "2", "3", "4", "5", "6", "7", "8", "9");
// Danner det antal tegn der ønskes inden for arrayet
for($i=0; $i < $count; $i++) {
$count_array = count($allowed);
$text = $text.$allowed[rand(0,$count_array)];
}
return $text;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?
$text = generate_text(8);
echo make_captcha(150, 50, $text, 20);
?>
</body>
</html>
