<?php
session_start(); //开启session
$strCode = "1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,z";
$strCode .= "A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";
$strList = explode(",", $strCode); //分割字符串
$StrMax = count($strList)-1; //获得字符的总个数
$verifyCode = "";
//随机取出四个字符
for($i=0;$i<4;$i++){
$strRnad = rand(0,$StrMax);
$verifyCode .= $strList[$strRnad];
}
$_SESSION['verifyCode'] = $verifyCode; //将字符串存入session中
$im = imagecreatetruecolor(100, 30); //创建画布
//加干扰直线和曲线
for($k=1;$k<=10;$k++){
imageline($im, rand(0,100), rand(0,30), rand(0,90), rand(0,30), imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)));
imagearc($im, rand(0,100), rand(0,30), rand(0,50), rand(0,15), rand(0,180), rand(0,90), imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)));
}
//加干扰点
for($j=1;$j<=100;$j++){
imagesetpixel($im, rand(2,98), rand(2,28), imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)));
}
//把上面取到的字符写入画布中
imagestring($im, rand(1,5), rand(0,75), rand(0,15), $verifyCode, imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)));
header("content-type: image/png");
imagepng($im); //输出图片
imagedestroy($im); //销毁图片
?>
php生成验证码代码
最新推荐文章于 2021-03-09 21:34:19 发布