首页 > 后端开发 > php教程 > 如何运用PHP GD库生成验证码_PHP教程

如何运用PHP GD库生成验证码_PHP教程

WBOY
发布: 2016-07-15 13:28:30
原创
809 人浏览过

当我们要使用先在php.ini里增加一行引用:extension=php_gd2.dll

重启apache。做一个测试页 var_dump(gd_info());输出数据表明PHP GD库引用成功。

表单auth.html

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"><span class="tag-name">html</span><span class="tag">></span></span></font></strong><span> </span></span></li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">head</span><span class="tag">></span></span></font></strong><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">meta</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">http-equiv</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'Content-Type'</font></span><span> </span><span class="attribute"><font color="#ff0000">content</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'text/html; charset=utf-8'</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">title</span><span class="tag">></span></span></font></strong><span>验证码</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span></font></strong><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span></font></strong><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">body</span><span class="tag">></span></span></font></strong><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">h1</span><span class="tag">></span></span></font></strong><span>请输入验证码</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">h1</span><span class="tag">></span></font></strong><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">form</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">action</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"check_auth.php"</font></span><span> </span><span class="attribute"><font color="#ff0000">method</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"post"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span>
</li>
<li class="alt">
<span>   </span><strong><font color="#006699"><span class="tag"><span class="tag-name">input</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"auth"</font></span><span> </span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"text"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span>
</li>
<li class="">
<span>   </span><strong><font color="#006699"><span class="tag"><span class="tag-name">img</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">src</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"auth.php"</font></span><span> </span><span class="attribute"><font color="#ff0000">border</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"0"</font></span><span> </span><span class="tag"><strong><font color="#006699">/></font></strong></span><span> </span>
</li>
<li class="alt">
<span>   </span><strong><font color="#006699"><span class="tag"><span class="tag-name">input</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"submit"</font></span><span> </span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"提交"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">form</span><span class="tag">></span></font></strong><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">body</span><span class="tag">></span></font></strong><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">html</span><span class="tag">></span></font></strong><span> </span>
</li>
</ol>
登录后复制

PHP GD库生成验证码 auth.php

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li>
<li class=""><span>   session_start();  </span></li>
<li class="alt"><span>   header("Content-type:image/png");  </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>   $</span><span class="attribute"><font color="#ff0000">img_width</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">100</font></span><span>;  </span>
</li>
<li class="">
<span>   $</span><span class="attribute"><font color="#ff0000">img_height</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">20</font></span><span>;  </span>
</li>
<li class="alt"><span> </span></li>
<li class=""><span>   srand(microtime()*100000);  </span></li>
<li class="alt">
<span>   for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;$i</span><strong><font color="#006699"><span class="tag"><span class="tag-name">4</span></span></font></strong><span>;$i++)  </span>
</li>
<li class=""><span>   {  </span></li>
<li class="alt">
<span>        $</span><span class="attribute"><font color="#ff0000">new_number.</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">dechex</font></span><span>(rand(0,15));  </span>
</li>
<li class=""><span>   }  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>   $_SESSION[check_auth]=$new_number;  </span></li>
<li class="alt">
<span>   $</span><span class="attribute"><font color="#ff0000">new_number</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">imageCreate</font></span><span>($img_width,$img_height);//创建图象  </span>
</li>
<li class=""><span>   ImageColorAllocate($new_number,255,255,255);  //设置背景色为白色  </span></li>
<li class="alt"><span> </span></li>
<li class="">
<span>   for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;$i</span><strong><font color="#006699"><span class="tag"><span class="tag-name">strlen</span></span></font></strong><span>($_SESSION[check_auth]);$i++)  </span>
</li>
<li class="alt"><span>   {  </span></li>
<li class="">
<span>       $</span><span class="attribute"><font color="#ff0000">font</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">mt_rand</font></span><span>(3,5);  </span>
</li>
<li class="alt">
<span>       $</span><span class="attribute"><font color="#ff0000">x</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">mt_rand</font></span><span>(1,8) + $img_width*$i/4;  </span>
</li>
<li class="">
<span>       $</span><span class="attribute"><font color="#ff0000">y</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">mt_rand</font></span><span>(1,$img_height/4);  </span>
</li>
<li class="alt">
<span>       $</span><span class="attribute"><font color="#ff0000">color</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">imageColorAllocate</font></span><span>($new_number,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));//设置字符颜色  </span>
</li>
<li class=""><span>       imageString($new_number,$font,$x,$y,$_SESSION[check_auth][$i],$color);//输出字符  </span></li>
<li class="alt"><span>   }  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>   ImagePng($new_number);  </span></li>
<li class=""><span>   ImageDestroy($new_number);  </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
登录后复制

PHP GD库提交页面 check_auth.php

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li>
<li class=""><span>   session_start();  </span></li>
<li class="alt">
<span>   $</span><span class="attribute"><font color="#ff0000">auth</font></span><span>=$_POST['auth'];  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>   if(empty($auth))  </span></li>
<li class=""><span>   {  </span></li>
<li class="alt"><span>       echo '错误:验证码不能为空';  </span></li>
<li class=""><span>       die;  </span></li>
<li class="alt"><span>   }  </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>   if($</span><span class="attribute"><font color="#ff0000">auth</font></span><span>==$_SESSION['check_auth'])  </span>
</li>
<li class=""><span>   {  </span></li>
<li class="alt"><span>       echo '正确';  </span></li>
<li class=""><span>   }  </span></li>
<li class="alt"><span>   else  </span></li>
<li class=""><span>   {  </span></li>
<li class="alt"><span>       echo '错误:验证码输入错误';  </span></li>
<li class=""><span>   }  </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
登录后复制

以上就是本文所介绍的PHP GD库生成验证码的相关知识,希望对大家有所帮助。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446427.htmlTechArticle当我们要使用 先在php.ini里增加一行引用:extension=php_gd2.dll 重启apache。做一个测试页 var_dump(gd_info());输出数据表明PHP GD库引用成功。 表单...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板