session_start();
session_register('safecode');
$type = 'gif';
$width= 40;
$height= 16;
header("content-type: image/".$type);
srand((double)microtime()*1000000);
$randval = randstr(4,"");
if($type!='gif' && function_exists('imagecreatetruecolor')){
$im = @imagecreatetruecolor($width,$height);
}else{
$im = @imagecreate($width,$height);
}
$r = array(225,211,255,223);
$g = array(225,236,237,215);
$b = array(225,236,166,125);
$key = rand(0,3);
$backcolor = imagecolorallocate($im,$r[$key],$g[$key],$b[$key]);//背景色(随机)
$bordercolor = imagecolorallocate($im, 0, 0, 0);//边框色
$pointcolor = imagecolorallocate($im, 255, 170, 255);//点颜色
@imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backcolor);//背景位置
@imagerectangle($im, 0, 0, $width-1, $height-1, $bordercolor); //边框位置
$stringcolor = imagecolorallocate($im, 255,51,153);
for($i=0;$i<=100;$i++){
$pointx = rand(2,$width-2);
$pointy = rand(2,$height-2);
@imagesetpixel($im, $pointx, $pointy, $pointcolor);
}
@imagestring($im, 3, 5, 1, $randval, $stringcolor);
$imagefun='image'.$type;
$imagefun($im);
@imagedestroy($im);
$_session['safecode'] = $randval;
//产生随机字符串
function randstr($len=6,$format='all') {
switch($format) {
case 'all':
$chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789www.111com.net'; break;
case 'char':
$chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; break;
case 'number':
$chars='0123456789'; break;
default :
$chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789';
break;
}
$string="";
while(strlen($string)<$len)
$string.=substr($chars,(mt_rand()%strlen($chars)),1);
return $string;
}
//调用此页面,如果下面的式子成立,则生成验证码图片
if($_get["action"]=="verifycode")
{
rand_create();
}
//验证码图片生成
function rand_create()
{
//通知浏览器将要输出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);
while(($randval=rand()%100000)<10000);{
$_session["login_check_num"] = $randval;
//将四位整数验证码绘入图片
imagestring($im, 5, 10, 3, $randval, $black);
}
//加入干扰象素
for($i=0;$i<200;$i++){
$randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
//输出验证图片
imagepng($im);
//销毁图像标识符
imagedestroy($im);
}
//检验验证码
function rand_check()
{
if($_post["reg_rand"] == $_session["login_check_num"]){
return true;
}
else{
exit("验证码输入错误");
}
}
|