thinkphp驗證碼的實作(form、ajax實作驗證)_php實例

WBOY
發布: 2016-08-17 13:02:37
原創
1158 人瀏覽過

兩種驗證碼驗證實作,一種直接在form表單提交按鈕實現驗證,一種使用ajax傳遞參數實現驗證:

1、直接在form表單提交按鈕實現驗證,在控制器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');//表单验证码
if($verify->check($code)){
$this->success('验证码正确');
}else{
$this->error('验证码错误');
}
}
public function verify()
{
// 实例化Verify对象
$verify = new \Think\Verify();
// 配置验证码参数
$verify->fontSize = 14; // 验证码字体大小
$verify->length = 4; // 验证码位数
$verify->imageH = 34; // 验证码高度
$verify->useImgBg = true; // 开启验证码背景
$verify->useNoise = false; // 关闭验证码干扰杂点
$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="验证码" style="width:100px;" />
<span style="right:120px;"></span>
<img class="verify" src="{:U(verify)}" alt="验证码" onClick="this.src=this.src+'&#63;'+Math.random()" />
</div>
<div class="col-xs-4">
<button type="submit" >验证</button>
</div>
</form>
</body>
</html> 
登入後複製

2、使用ajax傳遞參數實現驗證,在控制器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验证码获取
if($verify->check($code)){
$this->ajaxReturn(1);
}else{
$this->ajaxReturn(0);
}
}
public function verify()
{
// 实例化Verify对象
$verify = new \Think\Verify();
// 配置验证码参数
$verify->fontSize = 14; // 验证码字体大小
$verify->length = 4; // 验证码位数
$verify->imageH = 34; // 验证码高度
$verify->useImgBg = true; // 开启验证码背景
$verify->useNoise = false; // 关闭验证码干扰杂点
$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="验证码" style="width:100px;" />
<span style="right:120px;"></span>
<img class="verify" src="{:U(verify)}" alt="验证码" onClick="this.src=this.src+'&#63;'+Math.random()" />
</div>
<div class="col-xs-4">
<button type="button" id="ver">验证</button>
</div>
</form>
<script>
$(document).ready(function(){
/*ajax验证码*/
$("#ver").click(function(){
var code=$("#verify").val();//获取输入验证码
var url=$('form').attr('action');//获取表单action的值
$.ajax({
type:"post",
url:url,
data:{"code":code},
error:function(request){
alert("ajax错误");
},
success:function(data){
if(data){
alert("正确")
}else{
alert('错误')
}
}
});
});
});
</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驗證碼的實現(form、ajax使用驗證),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對腳本之家網站的支持!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!