咱們在登錄註冊的時候,爲了保證安全,常用到驗證碼,這能夠更好的起到保護做用,來看一下TP框架的驗證碼功能:php
一、首先在Controller文件夾下新建一個TestCroller.class.php文件:html
<?php namespace Home\Controller; use Think\Controller; class TestController extends Controller{ public function yanzheng(){ $this->show(); } public function yzm(){ $v = new \Think\Verify(); //$v->useImgBg = "true"; //$v->fontSize = "36"; //$v->imageW = "100"; //$v->imageH = "30"; //$v->length = "2"; $v->entry();//生成驗證碼 } }
二、在View文件夾下新建Test文件夾,在裏面建一個yanzheng.html頁面:jquery
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> <script src="__ROOT__/Public/js/jquery-3.2.0.min.js"></script> </head> <body> <h1>驗證碼</h1> <img src="__CONTROLLER__/yzm"/><input type="text" name="yzm" id="txt"/> <input type="button" id="btn" value="驗證"/> </body> </html>
三、判斷驗證碼:在上面的yanzheng.html頁面寫一個點擊事件,用ajax傳值來判斷:ajax
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> <script src="__ROOT__/Public/js/jquery-3.2.0.min.js"></script> </head> <body> <h1>驗證碼</h1> <img src="__CONTROLLER__/yzm" id="img" /><input type="text" name="yzm" id="txt"/> <input type="button" id="btn" value="驗證"/> </body> <script> $("#btn").click(function(){ var yzm = $("#txt").val(); $.ajax({ url:"__CONTROLLER__/yz", data:{yzm:yzm}, type:"POST", dataType:"TEXT", success:function(data){ if(data.trim() == "OK"){ alert("成功"); } else{ alert("失敗"); } } }); })
</script>
</html>安全
而後再回到控制器寫操做方法:框架
<?php namespace Home\Controller; use Think\Controller; class TestController extends Controller{ public function yanzheng(){ $this->show(); } public function yzm(){ $v = new \Think\Verify(); //$v->useImgBg = "true"; //$v->fontSize = "36"; //$v->imageW = "100"; //$v->imageH = "30"; //$v->length = "2"; $v->entry();//生成驗證碼 } public function yz(){ $yzm = $_POST["yzm"]; $v = new\Think\Verify(); if($v->check($yzm)){//驗證 $this->ajaxReturn("OK","eval"); } else{ $this->ajaxReturn("NO","eval"); } } }
看一下結果:dom
四、在實際運用中,驗證碼是能夠隨意刷新的,若是看不清當前的圖片能夠點擊刷新,便會更換驗證碼,咱們來看一下,只需一句話就能夠:ui
$("#img").click(function(){ var sjs = Math.floor(Math.random()*100);//添加隨機數 $(this) .attr("src","__CONTROLLER__/yzm/code/"+sjs); })
最終效果:this
五、多個驗證碼如何驗證:url
yanzheng.html:
<h1>驗證碼</h1> <img src="__CONTROLLER__/yzm" id="img"> <input type="text" name="yzm" id="txt"/> <input type="button" value="驗證" id="btn" /> <br /><br /> <!--第二個驗證碼--> <img src="__CONTROLLER__/yzm2" id="img">
TestController.class.php:
public function yzm(){$v = new \Think\Verify(); $v->entry(1); } public function yzm2(){$v = new \Think\Verify(); $v->entry(2); }
判斷驗證碼時;將標識寫入check($yzm,1):
public function yz(){ //取值 $yzm = $_POST["yzm"]; $v = new \Think\Verify(); //check()方法檢驗驗證碼是否正確 if($v->check($yzm,2)){ $this->ajaxReturn("OK","eval"); //若是正確,返回ok }else{ $this->ajaxReturn("NO","eval"); //若是不正確,返回no } }