> php教程 > php手册 > PHP图形验证码的具体实现方法

PHP图形验证码的具体实现方法

WBOY
풀어 주다: 2016-06-13 11:05:39
원래의
994명이 탐색했습니다.

我们现在在网站注册等地方经常会见到需要你输入验证码的请款,它就是将随机数字或符号以图片的形式展现在用户面前,并提供验证后才能使用相关功能。今天我们就介绍具体实现代码如下:

<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> </span></li>
<li class="alt"><span>class rndnum{  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>function rnd(){  </span></li>
<li class=""><span>srand((double)microtime()*1000000);  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">rnd_number</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>(  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">1</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'1',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">2</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'2',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">3</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'3',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">4</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'4',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">5</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'5',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">6</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'6',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">7</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'7',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">8</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'8',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">9</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'9',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">10</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'a',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">11</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'b',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">12</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'c',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">13</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'d',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">14</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'e',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">15</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'f',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">16</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'g',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">17</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'h',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">18</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'i',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">19</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'j',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">20</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'k',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">21</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'l',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">22</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'m',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">23</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'n',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">24</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'o',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">25</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'p',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">26</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'q',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">27</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'r',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">28</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'s',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">29</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'t',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">30</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'u',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">31</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'v',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">32</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'w',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">33</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'x',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">34</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'y',  </span>
</li>
<li class="">
<span></span><span class="attribute"><font color="#ff0000">35</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'z',  </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">36</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'0'  </span>
</li>
<li class=""><span>);  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">result</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array_rand</font></span><span>($rnd_number,6);  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">j</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">count</font></span><span>($result);  </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><span class="tag"><strong><font color="#006699"></font></strong></span><span>$j;$i++) {  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">re.</font></span><span>=$rnd_number[$result[$i]];  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class="">
<span>//$</span><span class="attribute"><font color="#ff0000">re</font></span><span>=$rnd_number[$result[1]].$rnd_number[$result[2]].$rnd_number[$result[3]].$rnd_number[$result[4]].$rnd_number[$result[5]].$rnd_number[$result[6]].$rnd_number[$result[7]];  </span>
</li>
<li class="alt"><span>//return array_keys($result);  </span></li>
<li class=""><span>return $re;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>/*用法  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">rndnum</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">new</font></span><span> rndnum();  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">num</font></span><span>=$rndnum-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>rnd();  </span>
</li>
<li class=""><span>echo $num;  </span></li>
<li class="alt"><span>*/  </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
로그인 후 복사

生成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> </span></li>
<li class="alt"><span>Header("Content-type: image/PNG");   </span></li>
<li class=""><span>require_once("rndnum.php");  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">rndnum</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">new</font></span><span> rndnum();  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">authnum</font></span><span>=$rndnum-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>rnd();  </span>
</li>
<li class="alt"><span>session_start();  </span></li>
<li class=""><span>$_SESSION["extrra_code"]=$authnum;  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">im</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">imagecreate</font></span><span>(72,20);   </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">black</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">ImageColorAllocate</font></span><span>($im, 0,0,0);   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">white</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">ImageColorAllocate</font></span><span>($im, 255,255,255);   </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">gray</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">ImageColorAllocate</font></span><span>($im, 200,200,200);   </span>
</li>
<li class="alt"><span>imagefill($im,0,0,$gray);    </span></li>
<li class=""><span>imagestring($im,5,10,3,$authnum,$black);   </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">200</span></span></font></strong><span>;$i++)   //加入干扰象素   </span>
</li>
<li class=""><span>{   </span></li>
<li class="alt">
<span>    $</span><span class="attribute"><font color="#ff0000">randcolor</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">ImageColorallocate</font></span><span>($im,rand(0,255),rand(0,255),rand(0,255));  </span>
</li>
<li class=""><span>    imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);   </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span>ImagePNG($im);   </span></li>
<li class="alt"><span>ImageDestroy($im);   </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
로그인 후 복사

以上就是全部PHP图形验证码的实现代码,希望对大家有所帮助。


원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿