index.php
[code]<?php
session_start();

if($_SERVER['REQUEST_METHOD'] == 'POST') {
	if(empty($_SESSION['captcha']) || empty($_POST[$_SESSION['captcha']['field']])) {
		die('You did not complete the anti-bot clock. Please try again.');
	}

	$_POST[$_SESSION['captcha']['field']] = trim($_POST[$_SESSION['captcha']['field']]);
	if(strpos($_POST[$_SESSION['captcha']['field']], ':') === false) {
		die('You incorrectly answered the anti-bot clock. Please try again.');
	}
	$parts = explode(':', $_POST[$_SESSION['captcha']['field']]);
	if(count($parts) != 2) {
		die('You incorrectly answered the anti-bot clock. Please try again.');
	}

	// Quick Sanitize integers by casting.  Oops. doing this removes prepending 0.
	$parts[0] = (int) $parts[0];
	$parts[1] = (int) $parts[1];

	// Re-Add 0 (be kind, the user may have missed it off) 1:25 instead of 01:25
	if($parts[0] < 10 && strlen($parts[0]) == 1) {
		$parts[0] = '0'.$parts[0];
	}
	if($parts[1] < 10 && strlen($parts[1]) == 1) {
		$parts[1] = '0'.$parts[1];
	}
	$answer = md5($parts[0].':'.$parts[1]) ;
	if ($_SESSION['captcha']['clock'] != $answer) {
		die('You incorrectly answered the anti-bot clock. Please try again.');
	}
	echo 'correct';
}

$range = range('a', 'z'); 
$field = $range[array_rand($range)];
$rand = rand(8,12);
for($i=0;$i<=$rand;$i++) {
	$field .= (rand(0, 1) == 0) ? $range[array_rand($range)] : rand(0, 9);
}
$_SESSION['captcha']['field'] = $field;

echo '<form action="index.php" method="post">
<img src="clock.php" alt="What is the time on the Clock?" /><br />
Please enter the time in the format 00:00 and to the nearest 5 minutes eg 04:25<br />
<a href="http://www.wikihow.com/Read-a-Clock" target="_blank" title="How to Read a Clock">How to Read a Clock</a><br />
<input type="text" name="'.$_SESSION['captcha']['field'].'" value="" size="30" /><br />
<input type="submit" name="submit" value="Submit" size="30" />
</form>

Please note that this script was originally created by <a href="http://custom.simplemachines.org/mods/index.php?mod=1134">karlbenson</a>, and you can view the source of this code <a href="source.php">here</a>.';
?>[/code]

clock.php
[code]<?php
session_start();

// Change the time, reset uses, but keep the same field
$sethour = 12 - rand(1,12) ;
$setminute = 12 - rand(1,12);

// Work Out the Answer
// - Minutes
// Less than 10 (2*5) means single digit, so turn it into double digits
if($setminute < 2) {
	$answer_minute = '0'.($setminute * 5);
} else {
	$answer_minute = $setminute * 5;
}
	
// - Hour (remember were using 12  not 24 hr clock)
if($sethour == 0) {
	$answer_hour = '12';
} elseif($sethour < 10) {
	$answer_hour = '0'.$sethour;
} else {
	$answer_hour = $sethour;
}

// - Answer : Store MD5'd to compare against
$_SESSION['captcha']['clock'] = md5($answer_hour.':'.$answer_minute);

// Tidy up
unset($answer_minute, $answer_hour);

// Now to Create the Clock

// Face selection
$rand = rand(1,5);
$face = './clocks/face'.$rand.'.png';
// Hands selection (Use same type for hour and minute hands)
$rand = rand(1,5);
$hour = './clocks/hour'.$rand.'.png';
$minute = './clocks/minute'.$rand.'.png';

// Rotation? Get our time *30 (less some shift hour hand depending on minutes)
$sethour = 360 - (($sethour * 30) + ($setminute*2.5));
$setminute = 360 - ($setminute * 30);

// -- The minute hand --
$minute = imagecreatefrompng($minute);

// Rotate if necessary
if(!empty($setminute))
	$minute = imagerotate($minute, $setminute, -1);

// Save Transparency/Alpha
imagealphablending($minute, true);
imagesavealpha($minute, true);

// Select random color
$col1 = rand(1,255);
$col2 = rand(1,255);
$col3 = rand(1,255);
// Too white?
if($col1 + $col2 + $col3 > 750)
{
	$col = rand(0,2);
	if($col == 0)
		$col1 = $col1 - rand(50,100);
	elseif($col == 1)
		$col2 = $col2 - rand(50,100);
	elseif($col == 2)
		$col3 = $col3 - rand(50,100);
}
// Colorize the minute hand
imagefilter($minute, IMG_FILTER_COLORIZE, $col1, $col2, $col3);

// -- The face --
$face = imagecreatefrompng($face);

// Add Some dots
for($i=0;$i<=1000;$i++)
{
	$color = imagecolorallocate($face, rand(0,256), rand(0,256), rand(0,256));
	imagesetpixel($face, rand(0,150) , rand(0,150) , $color);
}

//Get the sizes of both pix
$face_width=imagesx($face);
$face_height=imagesy($face);
$minute_width=imagesx($minute);
$minute_height=imagesy($minute);

$dest_x = ( $face_width / 2 ) - ( $minute_width / 2 );
$dest_y = ( $face_height / 2 ) - ( $minute_height / 2 );

// Merge the face with minute
imagecopy($face, $minute, $dest_x, $dest_y, 0, 0, $minute_width, $minute_height);

// Destroy the minute hand as its no longer needed
imagedestroy($minute);

// -- The hour hand --
$hour = imagecreatefrompng($hour);

// Rotate if necessary
if(!empty($sethour))
	$hour = imagerotate($hour, $sethour, -1);

// Save Transparency/Alpha
imagealphablending($hour, true);
imagesavealpha($hour, true);

// Colorize the hour (the same as we did the minute hand)
imagefilter($hour, IMG_FILTER_COLORIZE, $col1, $col2, $col3);

//Get the sizes of both pix
$hour_width = imagesx($hour);
$hour_height = imagesy($hour);
$dest_x = ( $face_width / 2 ) - ( $hour_width / 2 );
$dest_y = ( $face_height / 2 ) - ( $hour_height / 2 );

// Merge the hour onto the face
imagecopy($face, $hour, $dest_x, $dest_y, 0, 0, $hour_width, $hour_height);

// Destroy the hour hand as its no longer needed
imagedestroy($hour);

// Random Special Effects (PHP5 only)
if (@version_compare(PHP_VERSION, '5.0.0') !== -1)
{
	$rand = rand(0, 5);
	if($rand == 1) {
		imagefilter($face, IMG_FILTER_NEGATE);
	} elseif($rand == 2) {
		imagefilter($face, IMG_FILTER_GRAYSCALE);
	} elseif($rand == 3) {
		imagefilter($face, IMG_FILTER_EDGEDETECT);
	} elseif($rand == 4) {
		imagefilter($face, IMG_FILTER_EMBOSS);
	} elseif($rand == 5) {
		imagefilter($face, IMG_FILTER_MEAN_REMOVAL);
	}
}

// Headers - don't cache this
header("Cache-Control: no-cache");
header("Pragma: nocache");
header('Content-type: image/png');

// Send out the image
imagepng ($face);

// Destroy the images
imagedestroy($face);

// The end
die();
?>[/code]