一、制作思路。
由于注册码经常被用来防止机器恶意注册,这里我发布一个生成png图像验证码的基本图片,并分析一下简单的思路:
1.生成png图片2。设置图片的背景颜色3。设置字体颜色和样式4。生成4位随机验证码5。调整每个生成字符的旋转角度和位置,并将其绘制在png图片6上。添加噪音和干扰线,防止套准机分析原图恶意套准7。输出图片8。释放图片占用的内存。
二、实施方法。
Authcode.php档案
?PHP session _ start();标题(' Content-type : image/png ');//创建图片$im=imagecreate($x=130,$ y=45);$bg=imagecolorallocate($im,rand(50,200),rand(0,155),rand(0,155));//对imagecolorplocate()的第一次调用将基于调色板将背景色$ font color=imagecolorplocate($ im,255,255,255)填充到图像中;//font color $ font style=' rock . TTF ';//字体样式,可在文件夹c: windows Fonts 下找到。我把它和authcode.php文件放在同一个目录下,其他的字体样式可以替换。//的随机字符为($ I=0;$ I 4;$ I){ $ randascinumarray=array(rand(48,57),rand(65,90));$ randascinium=$ randascininumarray[rand(0,1)];$ randStr=chr($ randAsciiNum);imagettftext($im,30,rand(0,20)-rand(0,25),5 $i*30,rand(30,35),$fontColor,$fontstyle,$ randStr);$authcode。=$ randStr} $ _ SESSION[' auth code ']=$ randfourttr;//将用户与用户输入的验证码进行比较//干扰行为($ I=0;$ i8$ I){ $ line color=imagecolor allocate($ im,rand(0,255),rand(0,255),rand(0,255));imageline ($im,rand(0,$x),0,rand(0,$x),$y,$ line color);}//干扰点为($ I=0;$ i250$i ){ imagesetpixel($im,rand(0,$x),rand(0,$y),$ font color);} image png($ im);image destroy($ im);渲染:
以上就是php验证码的思路和实现方法,希望对大家的学习有所帮助。