<?php
session_start();
// generate 5 digit random number
$rand = rand(10000, 99999);

if ($_GET) {
$rand = rand(10000, 99999);
}
// create the hash for the random number and put it in the session
$_SESSION['image_random_value'] = md5($rand);

$width = 60;

$height = 30;

// create the image
$image = imagecreate($width, $height);

// use white as the background image
$bgColor = imagecolorallocate ($image, 0, 0, 0);

// the text color is white
$textColor = imagecolorallocate ($image, 255, 255, 255);

// the noise color is black
$noise_color = imagecolorallocate($image, 255, 0, 0);


for( $i=0; $i<($width*$height)/3; $i++ ) {

imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);

}

for( $i=0; $i<($width*$height)/150; $i++ ) {

imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);

}


// write the random number
imagestring ($image, 4, 5, 8, $rand, $textColor); 

// send several headers to make sure the image is not cached 
// taken directly from the PHP Manual

// Date in the past 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

// always modified 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

// HTTP/1.1 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false);

// HTTP/1.0 
header("Pragma: no-cache");


// send the content type header so the image is displayed properly
header('Content-type: image/jpeg');

// send the image to the browser
imagejpeg($image);

// destroy the image to free up the memory
imagedestroy($image);
?>