宝哥软件园

php生成验证码函数

编辑:宝哥软件园 来源:互联网 时间:2021-09-30

服务器端编程语言(专业超文本预处理器的缩写)生成验证码的函数,实用靠谱。先上下生成的验证码的效果图(这里生成的是全数字的验证码的示例效果):

下面是服务器端编程语言(专业超文本预处理器的缩写)生成验证码的源码:

?PHP session _ start();//session _ register(' CheckCode ');//PHP4.2以上版本不需要用会话_寄存器()注册会议变量$ type=' gif$ width=45$ height=20标题(“内容类型:图像/”).$ type);srand((双)微时间()* 1000000);if(isset($ _ GET[' action ']){ $ rand val=randStr(4,$ _ GET[' action ']);} else { $ rand val=rand str(4 ' ');} if($type!=' gif ' function _ exists(' imagecreatetrue color '){[email protected]($ width,$ height);}else{ [emailprotected]($width,$ height);} $r=Array(225,211,255,223);$g=Array(225,236,237,215);$ b=数组(225,236,166,125);$key=rand(0,3);$ backColor=ImageColorAllocate($ im,$r[$key],$g[$key],$ b[$ key]);//背景色(随机)$ borderColor=ImageColorAllocate($ im,127,157,185);//边框色$ point color=ImageColorAllocate($ im,255,170,255);//点颜色@ imagefiledrectangle($ im,0,0,$width - 1,$height - 1,$ backColor);//背景位置@imagerectangle($im,0,0,$width-1,$height-1,$ borderColor);//边框位置$ StringColor=imageColorAllocate($ im,255,51,153);对于($ I=0;$ i=100$i ){ $pointX=rand(2,$ width-2);$pointY=rand(2,$ height-2);@imagesetpixel($im,$pointX,$pointY,$ PointColor);} @imagestring($im,5,5,1,$randval,$ StringColor);$ImageFun='Image ' .$ type $ ImageFun($ im);@ image destroy($ im);$ _ SESSION[' CheckCode ']=$ rand val;函数randStr($len=6,$ format=' ALL '){ switch($ format){ case ' ALL ' ://生成包含数字和字母的验证码$ chars=' abcdefghijklmnopqrstuvwxyzabbcdefghijklmnopqrstuvwxyz 0123456789 ';打破;案例CHAR'://仅生成包含字母的验证码$ chars=' abcdefghijklmnopqrstuvwxyzabbcdefghijklmnopqrstuvwxyz ';打破;案例NUMBER'://仅生成包含数字的验证码$ chars=' 0123456789打破;默认值$ :个字符=' abcdefghijklmnopqrstuvwxyzabbdefghijklmnopqrstuvwxyz 0123456789 ';打破;} $ string=while(strlen($ string)$ len)$ string .=substr($chars),(mt_rand()%strlen($chars)),1);返回$ string}该函数的具体使用方法请看如下这个示例(这里是生成全数字的验证码):

img src='http:checkCode.php?action=NUMBER ' width=' 45 ' height=' 20 '/以上就是服务器端编程语言(专业超文本预处理器的缩写)如何生成验证码的实现函数,希望对大家的学习有所帮助。

更多资讯
游戏推荐
更多+