<?php session_start();//开启session功能 header("Cache-Control: no-cache, must-revalidate"); $im = imagecreate(60,30);//定义图片宽度和高度 $vcode=getVCode();//获取要显示的字符 $bg = imagecolorallocate($im, 255, 255, 255);//定义图片背景 $txt = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));//定义要显示字符的颜色 imagestring($im, 8, 0, 0, $vcode, $txt);//写入字符串到图片 header(Content-type: image/jpeg);//定义Content-type imagejpeg($im);//以JPEG格式显示图片 $_SESSION[vcode]=$vcode;//写入SESSION function getVCode(){ //随机生成用户指定个数的字符串 $codenum=4; $checkcode=""; $string="";//要显示的可选字符串,请自行定义; for($i=0;$i<$codenum;$i ) { $number=rand(0,2); switch($number){ //根据可选字符串可灵活定义; case 0 : $rand_number=rand(0,10);break; case 1 : $rand_number=rand(11,36);break; case 2 : $rand_number=rand(37,62);break; } $code=substr($string,$rand_number,1); $checkcode=$checkcode.$code; } return $checkcode; } ?> |