thinkphp5驗證碼處理
阿新 • • 發佈:2018-02-27
erro sage 3.2 clas 分享 idt 技術分享 字符集 var
1.確定項目目錄》vendor》topthink》think-captcha目錄存在
2.在config中添加驗證碼配置
//驗證碼配置 ‘captcha‘ => [ // 驗證碼字符集合 ‘codeSet‘ => ‘2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY‘, // 驗證碼字體大小(px) ‘fontSize‘ => 20, // 是否畫混淆曲線 ‘useCurve‘ => true, // 驗證碼圖片高度 ‘imageH‘ => 42, //是否添加雜點 ‘useNoise‘=>true, // 驗證碼圖片寬度 ‘imageW‘ => 148, // 驗證碼位數 ‘length‘ => 4, // 驗證成功後是否重置 ‘reset‘ => true ],
3.模板captcha.html裏輸出驗證碼
<!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 type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> </head> <body> 輸入驗證碼: <div> <img id="verify_img" src="{:captcha_src()}" alt="驗證碼" onclick="refreshVerify()"> <a href="javascript:refreshVerify()">點擊刷新</a> </div> <form class="layui-form" action="" > <input type="text" name = "verify"> <button class="layui-btn" lay-filter="checkcaptcha" lay-submit="" id="checkcaptcha" > 保存 </button> </form> <script> function refreshVerify() { var ts = Date.parse(new Date())/1000; var img = document.getElementById(‘verify_img‘); img.src = "{:captcha_src()}"; } </script> <script> $(function () { $("#checkcaptcha").on("click",function(){ $.ajax({ type: ‘POST‘, url: "{:url(‘test/checkcaptcha‘)}", data: $(".layui-form").serialize(), dataType: "json", async: false, error: function(request) { alert("發送請求失敗!"); }, success: function(data){ console.log(data); if (data.status == 1) { alert(data.message); } else { alert(data.message); } } }); }) }) </script> </body> </html>
4.在控制器Test.php中書寫驗證碼檢驗邏輯
//檢驗驗證碼
public function checkcaptcha()
{
$status=1;
$captcha = input(‘verify‘);
if(!captcha_check($captcha)){
//驗證碼錯誤
$message=‘驗證碼錯誤‘;
}else{
//驗證碼正確
$message=‘驗證碼正確‘;
}
return [‘status‘=> $status, ‘message‘=> $message];
}
thinkphp5驗證碼處理