>php教程 >php手册 >如何运用PHP GD库生成验证码

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

WBOY
WBOY원래의
2016-06-13 11:05:50929검색

当我们要使用先在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库生成验证码的相关知识,希望对大家有所帮助。


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.