Home > Backend Development > PHP Tutorial > Specific implementation method of PHP graphic verification code_PHP tutorial

Specific implementation method of PHP graphic verification code_PHP tutorial

WBOY
Release: 2016-07-15 13:28:36
Original
775 people have browsed it

We now often see requests for payment that require you to enter a verification code in website registration and other places. It displays random numbers or symbols in the form of pictures in front of users, and provides verification before they can use related functions. Today we will introduce the specific implementation code of as follows:

<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></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>re.</FONT></SPAN><SPAN>=$rnd_number[$result[$i]];  </SPAN></SPAN><LI class=alt><SPAN>}  </SPAN><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></SPAN><LI class=alt><SPAN>//return array_keys($result);  </SPAN><LI class=""><SPAN>return $re;  </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN>}  </SPAN><LI class=alt><SPAN>/*用法  </SPAN><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></SPAN><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>
Copy after login

Generate a picture of the PHP graphic verification code and fill it with random numbers:

<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><SPAN class=tag-name>200</SPAN></FONT></STRONG><SPAN>;$i++)   //加入干扰象素   </SPAN></SPAN><LI class=""><SPAN>{   </SPAN><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></SPAN><LI class=""><SPAN>    imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);   </SPAN><LI class=alt><SPAN>}   </SPAN><LI class=""><SPAN>ImagePNG($im);   </SPAN><LI class=alt><SPAN>ImageDestroy($im);   </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong><span> </span>
</li>
</ol>
Copy after login

The above is the implementation code of all PHP graphic verification codes. I hope it will be helpful to everyone.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446423.htmlTechArticleWe now often see requests for payment that require you to enter a verification code in website registration and other places. It is a random number. Or symbols are displayed in front of the user in the form of pictures and verification is provided...
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