首页  >  文章  >  php教程  >  php验证码制作 - IoveC

php验证码制作 - IoveC

WBOY
WBOY原创
2016-05-20 13:15:161046浏览

目标: 使用php生成验证码

成品:

    

逻辑代码: authcode.php

<span style="color: #000000;">php
</span><span style="color: #008080;">header</span>("Content-type:image/png"<span style="color: #000000;">);
</span><span style="color: #008080;">session_start</span><span style="color: #000000;">();
</span><span style="color: #008000;">//</span><span style="color: #008000;">$str用于存放验证码</span>
<span style="color: #800080;">$str</span>=""<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">$charset中剔除了0,o,1,l等易混淆字符</span>
<span style="color: #800080;">$cs</span>="abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345678923456789"<span style="color: #000000;">;
</span><span style="color: #800080;">$img</span>=imagecreate(<span style="color: #800080;">$width</span>=100,<span style="color: #800080;">$height</span>=26<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">图片背景色使用浅色调</span>
<span style="color: #800080;">$img_bg</span>=imagecolorallocate(<span style="color: #800080;">$img</span>,<span style="color: #008080;">rand</span>(186,255), <span style="color: #008080;">rand</span>(186,255), <span style="color: #008080;">rand</span>(186,255<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 style="color: #000000;">){
    </span><span style="color: #008000;">//</span><span style="color: #008000;">文字颜色使用深色调</span>
    <span style="color: #800080;">$txt_color</span>=imagecolorallocate(<span style="color: #800080;">$img</span>,<span style="color: #008080;">rand</span>(0,86),<span style="color: #008080;">rand</span>(0,86),<span style="color: #008080;">rand</span>(0,86<span style="color: #000000;">));
    </span><span style="color: #008000;">//</span><span style="color: #008000;">从$charset中随机出来一个字符</span>
    <span style="color: #800080;">$tmp</span>=<span style="color: #008080;">substr</span>(<span style="color: #800080;">$cs</span>,<span style="color: #008080;">rand</span>(0,<span style="color: #008080;">strlen</span>(<span style="color: #800080;">$cs</span>)-1),1<span style="color: #000000;">);
    </span><span style="color: #008000;">//</span><span style="color: #008000;">进行偏移和显示</span>
    imagestring(<span style="color: #800080;">$img</span>,5,<span style="color: #800080;">$i</span>*24+<span style="color: #008080;">rand</span>(6,15),<span style="color: #008080;">rand</span>(2,12),<span style="color: #800080;">$tmp</span>,<span style="color: #800080;">$txt_color</span><span style="color: #000000;">);
    </span><span style="color: #800080;">$str</span>.=<span style="color: #800080;">$tmp</span><span style="color: #000000;">;
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">将验证码值放入session中以备后用(验证用户输入的验证码)</span>
<span style="color: #800080;">$_SESSION</span>["authcode"]=<span style="color: #800080;">$str</span><span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">添加背景线条</span>
<span style="color: #0000ff;">for</span>(<span style="color: #800080;">$j</span>=0;<span style="color: #800080;">$j</span>$j<span style="color: #000000;">){
    </span><span style="color: #800080;">$line_color</span>=imagecolorallocate(<span style="color: #800080;">$img</span>,<span style="color: #008080;">rand</span>(150,210), <span style="color: #008080;">rand</span>(150,210), <span style="color: #008080;">rand</span>(150,210<span style="color: #000000;">));
    imageline(</span><span style="color: #800080;">$img</span>, <span style="color: #008080;">rand</span>(0, <span style="color: #800080;">$width</span>), <span style="color: #008080;">rand</span>(0, <span style="color: #800080;">$height</span>), <span style="color: #008080;">rand</span>(0, <span style="color: #800080;">$width</span>), <span style="color: #008080;">rand</span>(0, <span style="color: #800080;">$height</span>), <span style="color: #800080;">$line_color</span><span style="color: #000000;">);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">添加噪点</span>
<span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$i<span style="color: #000000;">){
    </span><span style="color: #800080;">$img_noisy</span>=imagecolorallocate(<span style="color: #800080;">$img</span>,<span style="color: #008080;">rand</span>(150,210), <span style="color: #008080;">rand</span>(150,210), <span style="color: #008080;">rand</span>(150,210<span style="color: #000000;">));
    imagesetpixel(</span><span style="color: #800080;">$img</span>,<span style="color: #008080;">rand</span>(0,<span style="color: #800080;">$width</span>),<span style="color: #008080;">rand</span>(0,<span style="color: #800080;">$height</span>),<span style="color: #800080;">$img_noisy</span><span style="color: #000000;">);
}
imagepng(</span><span style="color: #800080;">$img</span><span style="color: #000000;">);
imagedestroy(</span><span style="color: #800080;">$img</span>);


前端使用: register.html

<img  src="authcode.php" alt="php验证码制作 - IoveC" >

 

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn