首頁 >後端開發 >php教程 >PHP验证码在验证页面中的应用分析_PHP教程

PHP验证码在验证页面中的应用分析_PHP教程

WBOY
WBOY原創
2016-07-15 13:29:541013瀏覽

对于一个网站来说,肯定会需要一个提供验证功能的页面。那么我们现在就需要考虑验证页面中的验证码的应用了。我们接下来将要为大家具体讲解有关一、准备一个展示并提交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: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>
</li>
<li class=""><span>    </span></li>
<li class="alt"><span>    //打开session     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>    session_start();     </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>
<li class=""><span>    </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">html</span><span class="tag">></span></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<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=""><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> </span><span class="tag"><strong><font color="#006699">/></font></strong></span><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><span class="tag-name">title</span><span class="tag">></span></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 class="tag-name">body</span><span class="tag">></span></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       验证码:</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">/></span></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><span class="tag-name">iframe</span></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 class="tag-name">br</span><span class="tag">/></span></span></font></strong><span>    </span>
</li>
<li class=""><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">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">></font></strong></span><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       </span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></font></strong><span>    </span>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<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">"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">></font></strong></span><span>    </span>
</li>
<li class=""><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">"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">></font></strong></span><span>    </span>
</li>
<li class=""><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></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>

二、以下是PHP验证码生成页面,该页面在第一页面中被PHP验证码在验证页面中的应用分析_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/gif");     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>session_start();     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>//验证码为随机字符,以下是算法     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>$randval;     </span></li>
<li class=""><span>    </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">7</span></span></font></strong><span>;$i++){     </span>
</li>
<li class=""><span>    </span></li>
<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>
</li>
<li class=""><span>    </span></li>
<li class="alt"><span>    srand((double)microtime()*1000000);     </span></li>
<li class=""><span>    </span></li>
<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>
</li>
<li class=""><span>    </span></li>
<li class="alt">
<span>    if($randv%</span><span class="attribute"><font color="#ff0000">2</font></span><span>==0){     </span>
</li>
<li class=""><span>    </span></li>
<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>
</li>
<li class=""><span>    </span></li>
<li class="alt"><span>    }else{     </span></li>
<li class=""><span>    </span></li>
<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>
</li>
<li class=""><span>    </span></li>
<li class="alt"><span>    }     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>}     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>//注册PHP验证码到session     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>session_register($randval);     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>//以下是绘制验证码图     </span></li>
<li class=""><span>    </span></li>
<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>
</li>
<li class=""><span>    </span></li>
<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>
</li>
<li class=""><span>    </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">ImageCreateTrueColor</font></span><span>($width, $height);     </span>
</li>
<li class=""><span>    </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></li>
<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>
</li>
<li class=""><span>    </span></li>
<li class="alt"><span>ImageFill($im, 0, 0, $white);     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>srand((double)microtime()*1000000000);     </span></li>
<li class=""><span>    </span></li>
<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>
<li class=""><span>    </span></li>
<li class="alt"><span>srand((double)microtime()*1000000);     </span></li>
<li class=""><span>    </span></li>
<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>
<li class=""><span>    </span></li>
<li class="alt"><span>srand((double)microtime()*1000000);     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>ImageString($im,16 , mt_rand(0,$width - strlen($randval) * 10), mt_rand(0,$height-12), $randval, $blue);     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>ImageGIF($im);     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>ImageDestroy($im);     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>/*     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>需要注意的是:为了支持以上绘图函数,我们必须在PHP载入GD2图形处理库,可修改 php.ini 文件中的     </span></li>
<li class=""><span>    </span></li>
<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>
</li>
<li class=""><span>    </span></li>
<li class="alt"><span>为     </span></li>
<li class=""><span>    </span></li>
<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>
</li>
<li class=""><span>    </span></li>
<li class="alt"><span>即开启GD2库     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>*/     </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验证码是否通过验证

<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>
<li class=""><span>    </span></li>
<li class="alt"><span>    //开启session     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>    session_start();     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>    //得到用户输入的验证码,并转换成大写     </span></li>
<li class=""><span>    </span></li>
<li class="alt">
<span>    $</span><span class="attribute"><font color="#ff0000">imgId_req</font></span><span> = $_REQUEST['imgId'];     </span>
</li>
<li class=""><span>    </span></li>
<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>
</li>
<li class=""><span>    </span></li>
<li class="alt"><span>    //验证该字符串是否注册了session变量     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>    if (session_is_registered($imgId_req)) {     </span></li>
<li class=""><span>    </span></li>
<li class="alt">
<span>       echo "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">font</span></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">></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 class="tag-name">font</span></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">></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>    }     </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>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446325.htmlTechArticle对于一个网站来说,肯定会需要一个提供验证功能的页面。那么我们现在就需要考虑验证页面中的验证码的应用了。我们接下来将要为大家...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn