image.php主要是生成一个图片文件
- <?
- $sxscode = '';//初始化
- $sxscode_len = 4;//验证码长度
- //-----------
- //验证码范围,如果包含字母+数字组合则保留前一句,只需要数字则保留后一句
- $sxsarray = array('a','b','c','d','e','f','g','h','i','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0');
- //$sxsarray = array('1','2','3','4','5','6','7','8','9','0');
- //-----------
- //随机排序数列
- shuffle($sxsarray);
- //随机数种子,PHP版本>4.2 可去除
- function make_seed() {
- list($usec, $sec) = explode(' ', microtime());
- return (float)$sec + ((float)$usec * 100000);
- }
- mt_srand(make_seed());
- //------------------------------
- //获取随机数
- for( ;strlen($sxscode)<=$sxscode_len-1; ){
- $sxscode .= $sxsarray[mt_rand(0, count($sxsarray))];
- }
- //写入服务器session,需要服务器session支持
- @session_start();
- $_SESSION['sxscode'] = $sxscode;
- $_SESSION['sxscode_offtime'] = time() + 15 * 60;
- //绘图,输出
- $im = imagecreate(50,20);
- $white = ImageColorAllocate($im, 255,255,255);
- $black = ImageColorAllocate($im, 0,0,0);
- imagestring($im, 5, 6, 3, $sxscode, $black);
- header("Content-type: image/PNG");
- ImagePNG($im);
- ImageDestroy($im);
- ?>