溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

thinkphp驗(yàn)證碼怎么弄

發(fā)布時(shí)間:2021-05-18 14:30:08 來源:億速云 閱讀:145 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹thinkphp驗(yàn)證碼怎么弄,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

兩種驗(yàn)證碼驗(yàn)證實(shí)現(xiàn),一種直接在form表單提交按鈕實(shí)現(xiàn)驗(yàn)證,一種使用ajax傳遞參數(shù)實(shí)現(xiàn)驗(yàn)證:

1、直接在form表單提交按鈕實(shí)現(xiàn)驗(yàn)證,在控制器VerifyController.class.php中寫入如下代碼:

namespace Home\Controller;
use Think\Controller;
class VerifyController extends Controller {
public function index() {
$this->display();
}
public function checkLogin() {
$verify=new \Think\Verify();
$code=I('post.verify');//表單驗(yàn)證碼
if($verify->check($code)){
$this->success('驗(yàn)證碼正確');
}else{
$this->error('驗(yàn)證碼錯(cuò)誤');
}
}
public function verify()
{
// 實(shí)例化Verify對(duì)象
$verify = new \Think\Verify();
// 配置驗(yàn)證碼參數(shù)
$verify->fontSize = 14; // 驗(yàn)證碼字體大小
$verify->length = 4; // 驗(yàn)證碼位數(shù)
$verify->imageH = 34; // 驗(yàn)證碼高度
$verify->useImgBg = true; // 開啟驗(yàn)證碼背景
$verify->useNoise = false; // 關(guān)閉驗(yàn)證碼干擾雜點(diǎn)
$verify->entry();
}
}

在視圖Verify/index.html中的代碼如下:

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="{:U('verify/checkLogin')}" method="post">
<div class="form-group has-feedback">
<input type="text" name="verify" id="verify" placeholder="驗(yàn)證碼"  />
<span ></span>
<img class="verify" src="{:U(verify)}" alt="驗(yàn)證碼" onClick="this.src=this.src+'?'+Math.random()" />
</div>
<div class="col-xs-4">
<button type="submit" >驗(yàn)證</button>
</div>
</form>
</body>
</html>

2、使用ajax傳遞參數(shù)實(shí)現(xiàn)驗(yàn)證,在控制器VerifyController.class.php中的代碼如下:

namespace Home\Controller;
use Think\Controller;
class VerifyController extends Controller {
public function index() {
$this->display();
}
public function checkLogin() {
$verify=new \Think\Verify();
$code=$_POST['code'];//ajax驗(yàn)證碼獲取
if($verify->check($code)){
$this->ajaxReturn(1);
}else{
$this->ajaxReturn(0);
}
}
public function verify()
{
// 實(shí)例化Verify對(duì)象
$verify = new \Think\Verify();
// 配置驗(yàn)證碼參數(shù)
$verify->fontSize = 14; // 驗(yàn)證碼字體大小
$verify->length = 4; // 驗(yàn)證碼位數(shù)
$verify->imageH = 34; // 驗(yàn)證碼高度
$verify->useImgBg = true; // 開啟驗(yàn)證碼背景
$verify->useNoise = false; // 關(guān)閉驗(yàn)證碼干擾雜點(diǎn)
$verify->entry();
}
}

視圖Verify/index.html中的代碼如下:

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="__JS__/jquery-2.1.0.min.js" ></script>
</head>
<body>
<form action="{:U('verify/checkLogin')}" method="post">
<div class="form-group has-feedback">
<input type="text" name="verify" id="verify" placeholder="驗(yàn)證碼"  />
<span ></span>
<img class="verify" src="{:U(verify)}" alt="驗(yàn)證碼" onClick="this.src=this.src+'?'+Math.random()" />
</div>
<div class="col-xs-4">
<button type="button" id="ver">驗(yàn)證</button>
</div>
</form>
<script>
$(document).ready(function(){
/*ajax驗(yàn)證碼*/
$("#ver").click(function(){
var code=$("#verify").val();//獲取輸入驗(yàn)證碼
var url=$('form').attr('action');//獲取表單action的值
$.ajax({
type:"post",
url:url,
data:{"code":code},
error:function(request){
alert("ajax錯(cuò)誤");
},
success:function(data){
if(data){
alert("正確")
}else{
alert('錯(cuò)誤')
}
}
});
});
});
</script>
</body>
</html>

在第2種方法,不要忘記下載jquery.min.js文件下載地址:http://www.jq22.com/jquery-info122

在配置文件Common/conf/config.php中配置地址:

return array( 
/*地址替換*/
'TMPL_PARSE_STRING'=>array( 
'__JS__'=>__ROOT__.'/Public/JS',
),
);

以上是“thinkphp驗(yàn)證碼怎么弄”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)
AI