How to generate verification code in PHP

WBOY
Release: 2016-07-28 08:26:11
Original
1239 people have browsed it

The principle of generating verification code is very simple, just one word "draw". Yes, the verification code we need to draw has a background, numbers or letters.

The effect is as shown:
 PHP如何生成验证码

The steps are as follows:
1. Get a random verification code
Use the getCode function (customized), it will return a string.

2. Create an image resource and assign a color

<code><span>$m</span> = imagecreatetruecolor(<span>$width</span>,<span>$height</span>);</code>
Copy after login

imagecolorallocate, this is actually getting a color

3. Start painting
1). Fill the starting area in the upper left corner of the image with the background color

<code>imagefill(<span>$m</span>,<span>0</span>,<span>0</span>,<span>$bg</span>);</code>
Copy after login

2). Add a colored rectangle
imagerectangle

3). Add interference points and interference lines

4). Draw the obtained verification code string on the image

4. Output the image
1).Output directly to the browser
//Note that there can be no output before this function is executed, including spaces
//If the response header is not set, the page will have garbled characters instead of an image of the verification code
header(“Content-type:image/png”); //Set response header information
imagepng($m);

2). Output to file

<code>imagepng(<span>$m</span>,<span>'test.png'</span>);
</code>
Copy after login

5. Destroy the image
imagedestroy($m);

The code is as follows:

<code><span>/**
 * @param int $num  验证码的个数,默认为4
 * @param int $type 验证码的类型,0:纯数字,1:数字+小写字母 2:数字+大小写字母
 * @param bool $outFile 验证码是否输出到文件中
 * @return array 以数组形式返回验证码和验证码图片名字
 */</span>
function drawIdentifyCode(<span>$num</span>=<span>4</span>,<span>$type</span>=<span>0</span>,<span>$outFile</span> = true)
{
    <span>//绘制验证码</span><span>$code</span> = getCode(<span>$num</span>,<span>$type</span>);<span>//获取验证码</span><span>$width</span> = <span>$num</span><span>*35</span>;
    <span>$height</span> = <span>40</span>;
<span>//1.创建一个画图像资源、分配颜色</span><span>$m</span> = imagecreatetruecolor(<span>$width</span>,<span>$height</span>);
    <span>$c</span> = array(imagecolorallocate(<span>$m</span>,<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>)),
        imagecolorallocate(<span>$m</span>,<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>)),
        imagecolorallocate(<span>$m</span>,<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>)),
        imagecolorallocate(<span>$m</span>,<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>),<span>rand</span>(<span>0</span>,<span>255</span>)));
    <span>$bg</span> = imagecolorallocate(<span>$m</span>,<span>220</span>,<span>220</span>,<span>220</span>); <span>//背景颜色</span><span>//2.开始绘画</span><span>//在image图像左上角处开始区域填充</span>
    imagefill(<span>$m</span>,<span>0</span>,<span>0</span>,<span>$bg</span>);
<span>//添加一个有颜色的矩形框</span>
    imagerectangle(<span>$m</span>,<span>0</span>,<span>0</span>,<span>$width</span>-<span>1</span>,<span>39</span>,<span>$c</span>[<span>0</span>]);
<span>//添加干扰点</span><span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span><<span>400</span>;<span>$i</span>++)
        imagesetpixel(<span>$m</span>,<span>rand</span>(<span>0</span>,<span>$width</span>),<span>rand</span>(<span>0</span>,<span>30</span>),<span>$c</span>[<span>$i</span><span>%4</span>]);
<span>//添加干扰线</span><span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span><<span>5</span>;<span>$i</span>++)
        imageline(<span>$m</span>,<span>rand</span>(<span>0</span>,<span>$width</span>),<span>rand</span>(<span>0</span>,<span>30</span>),<span>rand</span>(<span>0</span>,<span>$width</span>),<span>rand</span>(<span>0</span>,<span>30</span>),<span>$c</span>[<span>$i</span><span>%4</span>]);
<span>//绘制验证码内容(一个一个字符绘制)</span><span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span><<span>$num</span>;<span>$i</span>++)
        imagettftext(<span>$m</span>,<span>28</span>,<span>rand</span>(-<span>50</span>,<span>50</span>),<span>15</span>+(<span>28</span><span>*$</span>i),<span>30</span>,<span>$c</span>[<span>$i</span><span>%4</span>],<span>"consola.ttf"</span>,<span>$code</span>[<span>$i</span>]);
<span>//3.输出图像</span><span>$fileName</span> = null;
    <span>if</span>(!<span>$outFile</span>)
    {
        <span>//注意此函数执行前不能有输出,空格也不行</span><span>//如果没有设置响应头,则页面会出现乱码,而不是一张验证码的图像</span>
        header(<span>"Content-type:image/png"</span>); <span>//设置响应头信息</span>
        imagepng(<span>$m</span>);
    }
    <span>else</span>
    {
        <span>$fileName</span> = time().<span>'.png'</span>;
        imagepng(<span>$m</span>,<span>$fileName</span>);
    }

<span>//4.销毁图片</span>
    imagedestroy(<span>$m</span>);
    <span>return</span> array(<span>$code</span>,<span>$fileName</span>);
}

<span>/**
 * @function 随机生成一个验证码的函数
 * @param $m: 验证码的个数(默认为4)
 * @param $type:验证码的类型:0:纯数字,1:数字+小写字母 2:数字+大小写字母
 * @return 返回字符串形式的验证码 
 */</span>
function getCode(<span>$m</span>=<span>4</span>,<span>$type</span>=<span>0</span>)
{
    <span>$str</span> = <span>"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"</span>;
    <span>$t</span> = array(<span>9</span>,<span>35</span>,strlen(<span>$str</span>)-<span>1</span>); <span>//验证码类型</span><span>//随机生成验证码所需内容</span><span>$c</span> = <span>""</span>;
    <span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span><<span>$m</span>;<span>$i</span>++)
        <span>$c</span>.=<span>$str</span>[<span>rand</span>(<span>0</span>,<span>$t</span>[<span>$type</span>])];
    <span>return</span><span>$c</span>;
}</code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces how PHP generates verification codes, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template