Location: PHPKode > scripts > php easy :: protected form scripts set > php-easy-protected-form-scripts-set/plaintext/codeimg.php
<?php
/*************************************************************************
php easy :: protected form scripts set - Code Image Generator
==========================================================================
Author:      php easy code, www.phpeasycode.com
Web Site:    http://www.phpeasycode.com
Contact:     hide@address.com
*************************************************************************/

function GenRandCode($length) {
	$out = "";
	for($i=0; $i<$length; $i++) {
		$out.= rand(0,9);
	}
	return $out;
}

session_start();
if(!$_SESSION["SecurityCode"]) { // new session
	$code = GenRandCode(4); // generate 4-digit code
	$_SESSION["SecurityCode"] = $code; // write code to session
}
else { // existing session
	$code = $_SESSION["SecurityCode"]; // read code from session
}
session_write_close();

if(!function_exists("imagecreate")) die("Cannot create image - GD library is not installed!");
$img = imagecreate(44,14); // create a 44*14 image
$bgcolor = imagecolorallocate($img,255,255,255); // create white background
$textcolor = imagecolorallocate($img,0,0,0); // create black text
imagecolortransparent($img, $bgcolor); // set background color as transparent
imagefill($img,0,0,$bgcolor); // fill the background
imagestring($img,5,6,1,$code,$textcolor); // write code to image
header("Content-type: image/png"); // send correct header for image type
header("Cache-control: no-cache, no-store"); // do not cache
imagepng($img); // output the image
imagedestroy($img); // destroy image to free out memory
?>
Return current item: php easy :: protected form scripts set