24 Mar 2012

Einfache Captcha-Generation im PHP

Simple Captcha-Generation in PHP. Use at your own risk!

<?php
session_start();

$alphanum = 'abcdefghjkmnpqrstuvwxyz0123456789';
$code = substr( str_shuffle( $alphanum ), 0, 5 );

$_SESSION['captcha'] = md5( $code );

$image = imagecreatetruecolor(80,40);
$bgCol = imagecolorallocate ( $image, 255, 255, 255 );
imagefill ( $image, 0, 0, $bgCol);

$x=10;$y=10;
for ($i=0;$i<5;$i++) {
	$r=rand(0,1);$b=rand(0,1);$g=rand(0,1);
	// only red, green, blue, black 
	// todo: generate nicer colors
	if ($r==1) {$b=0;$g=0;}
	if ($g==1) {$b=0;}
	$txtCol = imagecolorallocate ( $image, $r*200, $g*200, $b*200 );
	imagestring ( $image, 5, $x, $y+rand(-3,3), substr($code,$i,1), $txtCol );
	$x+=12;
}

header( 'Expires: Mon, 12 Jul 1999 01:01:00 GMT' );
header( 'Last-Modified: ' . gmdate( "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' );
header( 'Content-type: image/jpeg' );
imagejpeg( $image );
imagedestroy( $image );
?>
Schauen sie doch auch mal auf einen Sprung bei atac vorbei!