<?php
    session_start();
    //通知浏览器将要输出PNG图片
    header("Content-type: image/PNG");
    //准备好随机数发生器种子  
    //srand((double)microtime()*1000000);
    //准备图片的相关参数  
    $im = imagecreate(62,20);
    $black = imagecolorallocate($im, 0,0,0);  //RGB黑色标识符
    $white = imagecolorallocate($im, 255,255,255); //RGB白色标识符
    $gray = imagecolorallocate($im, 200,200,200); //RGB灰色标识符
    //开始作图    
    imagefill($im,0,0,$gray);
	$randval=random(4);
    $_SESSION["GetCode"] = strtolower($randval);
        //将四位整数验证码绘入图片  
    imagestring($im, 5, 10, 3, $randval, $black);
    //输出验证图片
    imagepng($im);
    //销毁图像标识符
    imagedestroy($im);

function random($length, $numeric = 0) {
	$seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
	$seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
	$hash = '';
	$max = strlen($seed) - 1;
	for($i = 0; $i < $length; $i++) {
		$hash .= $seed{mt_rand(0, $max)};
	}
	return $hash;
}
?>  