thinkphp驗(yàn)證碼的實(shí)現(xiàn)(form、ajax實(shí)現(xiàn)驗(yàn)證)
兩種驗(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)證碼" style="width:100px;" />
<span style="right:120px;"></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)證碼" style="width:100px;" />
<span style="right:120px;"></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)證碼的實(shí)現(xiàn)(form、ajax使用驗(yàn)證),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- TP5(thinkPHP5)框架基于ajax與后臺(tái)數(shù)據(jù)交互操作簡(jiǎn)單示例
- ThinkPHP5.1+Ajax實(shí)現(xiàn)的無刷新分頁功能示例
- thinkPHP5 ajax提交表單操作實(shí)例分析
- ThinkPHP中使用ajax接收json數(shù)據(jù)的方法
- ThinkPHP結(jié)合AjaxFileUploader實(shí)現(xiàn)無刷新文件上傳的方法
- ThinkPHP實(shí)現(xiàn)ajax仿官網(wǎng)搜索功能實(shí)例
- thinkphp中AJAX返回ajaxReturn()方法分析
- thinkphp中ajax與php響應(yīng)過程詳解
- ThinkPHP處理Ajax返回的方法
- thinkPHP+ajax實(shí)現(xiàn)統(tǒng)計(jì)頁面pv瀏覽量的方法
- thinkPHP5框架實(shí)現(xiàn)基于ajax的分頁功能示例
- TP5(thinkPHP5)框架使用ajax實(shí)現(xiàn)與后臺(tái)數(shù)據(jù)交互的方法小結(jié)
相關(guān)文章
深入淺析php中sprintf與printf函數(shù)的用法及區(qū)別
這篇文章主要介紹了php中sprintf與printf函數(shù)的用法及區(qū)別,涉及到printf函數(shù)、sprintf函數(shù)相關(guān)資料,需要的朋友可以參考下2016-01-01
PHP實(shí)現(xiàn)中國(guó)公民身份證號(hào)碼有效性驗(yàn)證示例代碼
這篇文章主要介紹了PHP實(shí)現(xiàn)中國(guó)公民身份證號(hào)碼有效性驗(yàn)證示例代碼,可以判斷身份證號(hào)碼的正確性,非常具有實(shí)用價(jià)值2017-05-05
不使用php api函數(shù)實(shí)現(xiàn)數(shù)組的交換排序示例
這篇文章主要介紹了不使用php api函數(shù)實(shí)現(xiàn)數(shù)組的交換排序示例,需要的朋友可以參考下2014-04-04
Laravel5.1 框架數(shù)據(jù)庫查詢構(gòu)建器用法實(shí)例詳解
這篇文章主要介紹了Laravel5.1 框架數(shù)據(jù)庫查詢構(gòu)建器用法,結(jié)合實(shí)例形式詳細(xì)分析了laravel5.1框架查詢構(gòu)造器相關(guān)原理、使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01
Laravel+Layer實(shí)現(xiàn)圖片上傳功能(整理篇)
這篇文章主要介紹了Laravel+Layer實(shí)現(xiàn)圖片上傳功能(整理篇),需要的朋友可以參考下2018-01-01

