Home > Backend Development > PHP Tutorial > Application Analysis of PHP Verification Code in Verification Page_PHP Tutorial

Application Analysis of PHP Verification Code in Verification Page_PHP Tutorial

WBOY
Release: 2016-07-15 13:29:54
Original
1002 people have browsed it

For a website, it will definitely need a page that provides verification function. Then we now need to consider the application of the verification code in the verification page. Next, we will explain to you in detail about 1. Prepare a page to display and submit PHP verification code

<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 class=""><SPAN>    </SPAN><LI class=alt><SPAN>    @header("content-type:text/html; </SPAN><SPAN class=attribute><FONT color=#ff0000>charset</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>UTF</FONT></SPAN><SPAN>-8");     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    //打开session     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    session_start();     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<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>
<li class=""><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></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>meta</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> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span></font></strong><span>PHP验证码示例</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=""><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></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></li>
<li class="alt">
<span>       验证码:</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>/></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>iframe</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>id</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"iimg"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>height</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"100"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>width</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>300</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>src</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"img.php"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>frameborder</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"0"</FONT></SPAN><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>iframe</SPAN><SPAN class=tag>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>/></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>button</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> </SPAN><SPAN class=attribute><FONT color=#ff0000>onclick</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"iimg.location.reload();"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>form</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>action</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"validate.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>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>           输入验证码:</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"imgId"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>style</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"width:60"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>           </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</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>></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<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=""><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></li>
<li class="alt">
<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>
Copy after login

2. The following is the PHP verification code generation page, which is< in the first page ;img> Call

<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 class=""><SPAN>    </SPAN><LI class=alt><SPAN>Header("Content-type: image/gif");     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>session_start();     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>//验证码为随机字符,以下是算法     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>$randval;     </SPAN><LI class=""><SPAN>    </SPAN><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>7</SPAN></FONT></STRONG><SPAN>;$i++){     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    $</SPAN><SPAN class=attribute><FONT color=#ff0000>randstr</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>mt_rand</FONT></SPAN><SPAN>(ord('A'),ord('Z'));     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    srand((double)microtime()*1000000);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    $</SPAN><SPAN class=attribute><FONT color=#ff0000>randv</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>mt_rand</FONT></SPAN><SPAN>(0,10);     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    if($randv%</SPAN><SPAN class=attribute><FONT color=#ff0000>2</FONT></SPAN><SPAN>==0){     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>       $</SPAN><SPAN class=attribute><FONT color=#ff0000>randval.</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>mt_rand</FONT></SPAN><SPAN>(0,10);     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    }else{     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>       $</SPAN><SPAN class=attribute><FONT color=#ff0000>randval.</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>chr</FONT></SPAN><SPAN>($randstr);     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    }     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>}     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>//注册PHP验证码到session     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>session_register($randval);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>//以下是绘制验证码图     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>height</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>50</FONT></SPAN><SPAN>;//图高     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>width</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>100</FONT></SPAN><SPAN>;//图宽     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>im</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>ImageCreateTrueColor</FONT></SPAN><SPAN>($width, $height);     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><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></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>blue</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>ImageColorAllocate</FONT></SPAN><SPAN>($im, 0, 0, 64);     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>ImageFill($im, 0, 0, $white);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>srand((double)microtime()*1000000000);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>ImageLine($im, mt_rand(0,$width/3), mt_rand(0,$height/3), mt_rand($width/3,$width), mt_rand($height/3,$height), $blue);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>srand((double)microtime()*1000000);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>ImageLine($im, mt_rand($width/3,$width), mt_rand(0,$height/3), mt_rand(0,$width/3), mt_rand(0,$height/3), $blue);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>srand((double)microtime()*1000000);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>ImageString($im,16 , mt_rand(0,$width - strlen($randval) * 10), mt_rand(0,$height-12), $randval, $blue);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>ImageGIF($im);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>ImageDestroy($im);     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>/*     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>需要注意的是:为了支持以上绘图函数,我们必须在PHP载入GD2图形处理库,可修改 php.ini 文件中的     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>;</SPAN><SPAN class=attribute><FONT color=#ff0000>extension</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>php_gd2</FONT></SPAN><SPAN>.dll     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>为     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN></SPAN><SPAN class=attribute><FONT color=#ff0000>extension</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>php_gd2</FONT></SPAN><SPAN>.dll     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>即开启GD2库     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>*/     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span>    </span>
</li></ol>
Copy after login

3. This is the verification page, prompting whether the PHP verification code has passed the verification

<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> @header("content-type:text/html; </SPAN><SPAN class=attribute><FONT color=#ff0000>charset</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>UTF</FONT></SPAN><SPAN>-8");     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    //开启session     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    session_start();     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    //得到用户输入的验证码,并转换成大写     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    $</SPAN><SPAN class=attribute><FONT color=#ff0000>imgId_req</FONT></SPAN><SPAN> = $_REQUEST['imgId'];     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    $</SPAN><SPAN class=attribute><FONT color=#ff0000>imgId_req</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>strtoupper</FONT></SPAN><SPAN>($imgId_req);     </SPAN></SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    //验证该字符串是否注册了session变量     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>    if (session_is_registered($imgId_req)) {     </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>       echo "</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>font</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>color</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>blue</FONT></SPAN><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span>通过验证!</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>font</SPAN><SPAN class=tag>></span></font></strong><span>";     </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><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>font</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>color</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>red</FONT></SPAN><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>验证错误!</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>font</SPAN><SPAN class=tag>></span></font></strong><span>";     </span>
</li>
<li class=""><span>    </span></li>
<li class="alt"><span>    }     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>    //关闭session,以清除所有注册过的变量     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>    session_destroy();     </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>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446325.htmlTechArticleFor a website, it will definitely need a page that provides verification function. Then we now need to consider the application of the verification code in the verification page. What we are going to do next is...
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