Home >php教程 >php手册 >php验证码制作 - IoveC

php验证码制作 - IoveC

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-20 13:15:161089browse

目标: 使用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" >

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn