Maison >php教程 >php手册 >【代码实现】PHP生成各种随机验证码

【代码实现】PHP生成各种随机验证码

WBOY
WBOYoriginal
2016-06-06 19:57:401383parcourir

PHP生成验证码图片 PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中。PHP生成验证码的大致流程有: 1、产生一张png的图片; 2、为图片设置背景色; 3、设置字体颜色和样式; 4、产生4位数的随机的验证码; 5、把产

PHP生成验证码图片

PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中。PHP生成验证码的大致流程有:

1、产生一张png的图片;

2、为图片设置背景色;

3、设置字体颜色和样式;

4、产生4位数的随机的验证码;

5、把产生的每个字符调整旋转角度和位置画到png图片上;

6、加入噪点和干扰线防止注册机器分析原图片来恶意破解验证码;

7、输出图片;

8、释放图片所占内存。

示例代码。

<code> 
session_start(); 
getCode(<span>4</span>,<span>60</span>,<span>20</span>); 
 
<span>function</span> getCode(<span>$</span><span>num</span>,<span>$</span><span>w</span>,<span>$</span><span>h</span>) { 
    <span>$</span><span>code</span> = <span>""</span>; 
    <span>for</span> (<span>$</span><span>i</span> = <span>0</span>; <span>$</span><span>i</span> $<span>num</span>; <span>$</span><span>i</span>++) { 
        <span>$</span><span>code</span> .= rand(<span>0</span>, <span>9</span>); 
    } 
    <span>//4位验证码也可以用rand(1000,9999)直接生成</span> 
    <span>//将生成的验证码写入session,备验证时用</span> 
    <span>$_SESSION</span>[<span>"helloweba_num"</span>] = <span>$</span><span>code</span>; 
    <span>//创建图片,定义颜色值</span> 
    header(<span>"Content-type: image/PNG"</span>); 
    <span>$</span><span>im</span> = imagecreate(<span>$</span><span>w</span>, <span>$</span><span>h</span>); 
    <span>$</span><span>black</span> = imagecolorallocate(<span>$</span><span>im</span>, <span>0</span>, <span>0</span>, <span>0</span>); 
    <span>$</span><span>gray</span> = imagecolorallocate(<span>$</span><span>im</span>, <span>200</span>, <span>200</span>, <span>200</span>); 
    <span>$</span><span>bgcolor</span> = imagecolorallocate(<span>$</span><span>im</span>, <span>255</span>, <span>255</span>, <span>255</span>); 
    <span>//填充背景</span> 
    imagefill(<span>$</span><span>im</span>, <span>0</span>, <span>0</span>, <span>$</span><span>gray</span>); 
 
    <span>//画边框</span> 
    imagerectangle(<span>$</span><span>im</span>, <span>0</span>, <span>0</span>, <span>$</span><span>w</span><span>-1</span>, <span>$</span><span>h</span><span>-1</span>, <span>$</span><span>black</span>); 
 
    <span>//随机绘制两条虚线,起干扰作用</span> 
    <span>$</span><span>style</span> = <span>array</span> (<span>$</span><span>black</span>,<span>$</span><span>black</span>,<span>$</span><span>black</span>,<span>$</span><span>black</span>,<span>$</span><span>black</span>, 
        <span>$</span><span>gray</span>,<span>$</span><span>gray</span>,<span>$</span><span>gray</span>,<span>$</span><span>gray</span>,<span>$</span><span>gray</span> 
    ); 
    imagesetstyle(<span>$</span><span>im</span>, <span>$</span><span>style</span>); 
    <span>$</span><span>y1</span> = rand(<span>0</span>, <span>$</span><span>h</span>); 
    <span>$</span><span>y2</span> = rand(<span>0</span>, <span>$</span><span>h</span>); 
    <span>$</span><span>y3</span> = rand(<span>0</span>, <span>$</span><span>h</span>); 
    <span>$</span><span>y4</span> = rand(<span>0</span>, <span>$</span><span>h</span>); 
    imageline(<span>$</span><span>im</span>, <span>0</span>, <span>$</span><span>y1</span>, <span>$</span><span>w</span>, <span>$</span><span>y3</span>, IMG_COLOR_STYLED); 
    imageline(<span>$</span><span>im</span>, <span>0</span>, <span>$</span><span>y2</span>, <span>$</span><span>w</span>, <span>$</span><span>y4</span>, IMG_COLOR_STYLED); 
 
    <span>//在画布上随机生成大量黑点,起干扰作用;</span> 
    <span>for</span> (<span>$</span><span>i</span> = <span>0</span>; <span>$</span><span>i</span> 80; <span>$</span><span>i</span>++) { 
        imagesetpixel(<span>$</span><span>im</span>, rand(<span>0</span>, <span>$</span><span>w</span>), rand(<span>0</span>, <span>$</span><span>h</span>), <span>$</span><span>black</span>); 
    } 
    <span>//将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成</span> 
    <span>$</span><span>strx</span> = rand(<span>3</span>, <span>8</span>); 
    <span>for</span> (<span>$</span><span>i</span> = <span>0</span>; <span>$</span><span>i</span> $<span>num</span>; <span>$</span><span>i</span>++) { 
        <span>$</span><span>strpos</span> = rand(<span>1</span>, <span>6</span>); 
        imagestring(<span>$</span><span>im</span>, <span>5</span>, <span>$</span><span>strx</span>, <span>$</span><span>strpos</span>, substr(<span>$</span><span>code</span>, <span>$</span><span>i</span>, <span>1</span>), <span>$</span><span>black</span>); 
        <span>$</span><span>strx</span> += rand(<span>8</span>, <span>12</span>); 
    } 
    imagepng(<span>$</span><span>im</span>);<span>//输出图片</span> 
    imagedestroy(<span>$</span><span>im</span>);<span>//释放图片所占内存</span> 
} 
</code>

代码中,自定义函数getCode()诠释了验证码的生成过程。运用PHP GD库自带的图像处理函数,能轻松生成各种想要的图片效果。

imagecreate():创建一个新图像

imagecolorallocate():为图像分配颜色

imagefill():填充图像

imagerectangle():画一个矩形(边框)

imagesetstyle():设置画线风格

imageline():画一条线段

imagesetpixel():画点像素

imagepng():以PNG格式将图像输出到浏览器或文件

imagedestroy():释放图片所占内存

将上述代码保存为code_num.php,以便调用。

Ajax刷新和验证

验证码生成后,我们要在实际的项目中应用,通常我们使用ajax可以实现点击验证码时刷新生成新的验证码(有时生成的验证码肉眼很难识别),即“看不清换一张”。填写验证码后,还需要验证所填验证码是否正确,验证的过程是要后台程序来完成,但是我们也可以通过ajax来实现无刷新验证。

我们建立一个前端页面index.html,载入jquery,同时在body中加入验证码表单元素:

<code> 
<span><p><span>></span>验证码:<span><input> <span>type</span>=<span>"text"</span> <span>class</span>=<span>"input"</span> <span>id</span>=<span>"code_num"</span> <span>name</span>=<span>"code_num"</span> <span>maxlength</span>=<span>"4"</span> <span>/></span>  
<span><img  alt="【代码实现】PHP生成各种随机验证码" > <span>src</span>=<span>"code_num.php"</span> <span>id</span>=<span>"getcode_num"</span> <span>title</span>=<span>"看不清,点击换一张"</span> <span>align</span>=<span>"absmiddle"</span><span>></span><span></span></span></span></p></span> 
<span><p><span>></span><span><input> <span>type</span>=<span>"button"</span> <span>class</span>=<span>"btn"</span> <span>id</span>=<span>"chk_num"</span> <span>value</span>=<span>"提交"</span> <span>/></span><span></span></span></p></span> 
</code>

html代码中,【代码实现】PHP生成各种随机验证码

<code> 
$(<span>function</span>()<span>{</span> 
    <span>//数字验证</span> 
    $(<span>"#getcode_num"</span>).click(<span>function</span>()<span>{</span> 
        $(<span>this</span>).attr(<span>"src"</span>,<span>'code_num.php?'</span> + <span>Math</span>.random()); 
    <span>}</span>); 
    ... 
<span>}</span>); 
</code>

刷新验证码,其实就是重新请求了验证码生成程序,这里要注意的是调用code_num.php时要带上随机参数防止缓存。接下来填写好验证码之后,点“提交”按钮,通过$.post(),前端向后台chk_code.php发送ajax请求。

<code> 
$(<span>function</span>()<span>{</span> 
    ... 
    $(<span>"#chk_num"</span>).click(<span>function</span>()<span>{</span> 
        <span>var</span> code_num = $(<span>"#code_num"</span>).val(); 
        $.post(<span>"chk_code.php?act=num"</span>,<span>{</span>code:code_num<span>}</span>,<span>function</span>(msg)<span>{</span> 
            <span>if</span>(msg==<span>1</span>)<span>{</span> 
                alert(<span>"验证码正确!"</span>); 
            <span>}</span><span>else</span><span>{</span> 
                alert(<span>"验证码错误!"</span>); 
            <span>}</span> 
        <span>}</span>); 
    <span>}</span>); 
<span>}</span>); 
</code>

后台chk_code.php验证:

<code> 
session_start(); 
 
<span>$</span><span>code</span> = trim(<span>$_POST</span>[<span>'code'</span>]); 
<span>if</span>(<span>$</span><span>code</span>==<span>$_SESSION</span>[<span>"helloweba_num"</span>]){ 
   <span>echo</span> <span>'1'</span>; 
} 
</code>

后台根据提交的验证码与保存在session中的验证码比对,完成验证。

对于其他几种验证的生成和使用,其原理一样,开发者可以根据需要,产生多种样式的随机验证码,本文演示demo中提供了数字验证码、数字+字母验证码、中文验证码、仿google验证码,算术验证码等。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn