tp框架實現驗證碼驗證


//實現驗證頁面

<!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>
</head>
<script src="__PUBLIC__/js/jquery-1.11.2.min.js"></script>
<body>
<form action="__ACTION__" method="post">
<img src="__CONTROLLER__/yzm" id="img" />
<input type="text" name="yzm" />
<input  type="submit" value="注冊"/>

</form>
<script>
$("#img").click(function(){
    var a = Math.ceil(Math.random()*10);//如果瀏覽器不兼容可以設置隨機數(Math.ceil)是四舍五入化為整數
    $(this).attr("src","__CONTROLLER__/yzm/a/"+a+"");//設點擊事件從新設置src屬性($(this).attr("src","__CONTROLLER__))如果是兼容性好的(瀏覽器可以使用)
    //如果兼容不好的不許用生成隨機數的方式如($(this).attr("src","__CONTROLLER__/yzm/a/"+a+"");)
    })
</script>
</body>
</html>

//調方法實現驗證<?php

namespace Home\Controller; use Think\Controller; class TestController extends Controller { public function yanzhengma() { if(empty($_POST)) { $this->show(); } else { $Verify= new \Think\Verify();//掉方法 
        var_dump($Verify->check($_POST["yzm"]));//輸出在該驗證碼的常量如果為true成功 為false失敗 
 } } public function yzm() { $Verify= new \Think\Verify(); $Verify->fontSize=30;//輸出驗證碼的大小
//$Verify->length=2;//輸出驗證碼的長度:如果=2就有2個隨機數(默認是6位數)

//$Verify->fontttf="5.ttf";//輸出為5.ttf的字體 //$Verify->useImgBg=true;//開啟驗證碼背景圖片功能 隨機使用Thinkphp/Library/Think/Verify/bgs目錄下的隨機數
$Verify->useZh=true;//使用中文驗證碼,可以設置為:true在(Think/Library/Think/Verify/zhttf)的目錄下設置中文字體

$Verify->entry();//生成驗證碼 } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM