说明:一些基本的代码我都进行了注释,这里实现的验证码位数、需要用的字符串都可以再设置。有我的注释,大家应该很容易能看得懂。
基本思路:
1.用mt_rand()随机生成数字确定需要获取的字符串,对字符串进行拼接(觉得生成的验证码觉得有点太挤,大家可以再字符串中间拼接个空格键),实现随机验证码;
备注:建议大家用mt_rand(),而不是rand(),前者效率更高
2.利用gd库生成图片,把随机字符串写到图片输出。
效果:
每次刷新,都生成一个随机验证,后期我可能还会补充怎么实现随机码点击图片就再次更新
代码:
<span style="font-size: 18px;"><span style="color: #000000;">php </span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建画布</span> <span style="color: #800080;">$width</span> = 120; <span style="color: #008000;">//</span><span style="color: #008000;"> 规定画布的宽高</span> <span style="color: #800080;">$height</span> = 45<span style="color: #000000;">; </span><span style="color: #800080;">$image</span> = imagecreatetruecolor(<span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span>); <span style="color: #008000;">//</span><span style="color: #008000;"> 创建一幅真彩色图像 // 添加一些即将用到的颜色</span> <span style="color: #800080;">$white</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 0xf2, 0xec, 0xe0<span style="color: #000000;">); </span><span style="color: #800080;">$orange</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 0xff, 0xa5, 0x4c<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 对画布背景填充颜色</span> imagefill(<span style="color: #800080;">$image</span>, 0, 0, <span style="color: #800080;">$white</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">mt_rand 获取随机数 mt_rand(min, max);</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> str_rand(){ </span><span style="color: #800080;">$str</span>="abcdefghijkmnpqrstuvwxyz0123456789ABCDEFGHIGKLMNPQRSTUVWXYZ"<span style="color: #000000;">; </span><span style="color: #800080;">$rand</span>=""<span style="color: #000000;">; </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0; <span style="color: #800080;">$i</span>$i</span>++){<span style="color: #008000;">//</span><span style="color: #008000;">获取5个随机的字符串</span> <span style="color: #800080;">$rand</span> .= <span style="color: #800080;">$str</span>[<span style="color: #008080;">mt_rand</span>(0, <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$str</span>)-1)]; <span style="color: #008000;">//</span><span style="color: #008000;">如:随机数为30 则:$str[30]</span> <span style="color: #000000;"> } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$rand</span><span style="color: #000000;">; } </span><span style="color: #800080;">$verifyCode</span>=<span style="color: #000000;">str_rand(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 画一串字符串在画布上</span> imagestring(<span style="color: #800080;">$image</span>, 10, 10, 10, "<span style="color: #800080;">$verifyCode</span>", <span style="color: #800080;">$orange</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通知浏览器输出的是图像(png类型)</span> <span style="color: #008080;">header</span>('Content-Type: image/png'<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 输出到浏览器</span> imagepng(<span style="color: #800080;">$image</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 释放图像资源</span>