Return to Snippet

Revision: 42026
at February 26, 2011 02:39 by stz184


Initial Code
session_start();
header("Content-type: image/png");
$width = 146;
$height = 30;

$im = @imagecreate($width, $height)or die("Cannot Initialize new GD image stream");

imagecolorallocate($im, 255, 250, 255);
$noise_color = imagecolorallocate($im, 207, 239, 250);
for($i=0; $i<($width*$height)/3; $i++) {
	imagefilledellipse($im, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}

/* generate random lines in background */
for($i=0; $i<($width*$height)/150; $i++ ) {
 imageline($im, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}

$text_color[0] = imagecolorallocate($im, 255, 0, 0);
$text_color[1] = imagecolorallocate($im, 51, 166, 207);

$text = mb_substr(uniqid(mt_rand(), true), 0, 6);
unset($_SESSION['captcha']);
$_SESSION['captcha']['blue'] = $_SESSION['captcha']['red'] = '';

for($j = 0; $j < mb_strlen($text); $j++) {
	imagettftext($im, 20, 0, 5+($j*23), 24, $text_color[$j%2], './offshore.ttf', $text[$j]);
	if(($j%2) == 1) {
		$_SESSION['captcha']['blue'] .= $text[$j];
	}
	else {
		$_SESSION['captcha']['red'] .= $text[$j];
	}
}
imagepng($im);
imagedestroy($im);

Initial URL
http://vladimir-ivanov.net

Initial Description
Tell the users to enter only the red or only the blue digits, not everything they see.
Most bots will try to enter everything, displayed on the images.
To check is captcha code entered correctly, use $_SESSION['captcha']['red'] to check the red digits only and $_SESSION['captcha']['blue'] to check the blue digits only. Good luck !

Initial Title
Captcha with color protection

Initial Tags

                                

Initial Language
PHP