thinkphp自帶驗(yàn)證碼
前端頁(yè)面:
<div style="position:absolute;z-index:3;top:160px;left:180px;"> <img src="/static/imghw/default1.png" data-src="{:U('Verify')}" class="lazy" style="max-width:90%" onclick="this.src=this.src+'?'+Math.random()" id="safecode" style="height:50px;width:70%;"/ alt="thinkphp verification code" ></div>
//驗(yàn)證碼判斷 public function Verify(){ ob_clean(); //顯示驗(yàn)證碼 $cfg=array( 'codeSet' => '0123456789', // 驗(yàn)證碼字符集合 'imageH' => 25, // 驗(yàn)證碼圖片高度 'imageW' => 80, // 驗(yàn)證碼圖片寬度 'length' => 4, // 驗(yàn)證碼位數(shù) 'fontttf' => '4.ttf', // 驗(yàn)證碼字體,不設(shè)置隨機(jī)獲取 'fontSize' => 10, // 驗(yàn)證碼字體大小(px) 'useNoise' => false, // 是否添加雜點(diǎn) 'useCurve' => false, // 是否畫混淆曲線 'bg' => array(226,229,236) //背景顏色 ); $very=new \Think\Verify($cfg); $very->entry(); } //客戶端通過(guò)ajax,實(shí)現(xiàn)校驗(yàn)驗(yàn)證碼 public function checkVerify(){ $code = I('get.code'); $very = new \Think\Verify(); $key = $this->auth_my_code($very,$very->seKey); // 驗(yàn)證碼不能為空 $secode = session($key); //對(duì)$code進(jìn)行加密,在比較校驗(yàn) if($this->auth_my_code($very,strtoupper($code)) == $secode['verify_code']) { echo json_encode(array('flag'=>1,'cont'=>'驗(yàn)證碼正確')); }else{ echo json_encode(array('flag'=>2,'cont'=>'驗(yàn)證碼錯(cuò)誤')); } } private function auth_my_code($vry,$str){ $key = substr(md5($vry->seKey), 5, 8); $str = substr(md5($str), 8, 10); return md5($key . $str); }
以上驗(yàn)證碼如果輸入錯(cuò)誤提交后不能自動(dòng)刷新,對(duì)代碼進(jìn)行更改后:
location.href="/Login/Login";這樣只能對(duì)整個(gè)頁(yè)面刷新,提交表單的值可能會(huì)丟失,非常影響用戶體驗(yàn)。
2.如果驗(yàn)證碼輸入錯(cuò)誤,提交后自動(dòng)刷新驗(yàn)證碼。
else{ $('#safecode').attr("src","/Login/Verify?"+Math.random()); NewAlert(2,"驗(yàn)證碼有誤,請(qǐng)重新輸入",null); code_ok = false; $('#verifyresult').html(msg.cont).css({'color':'red','font-size':'12px'}); }
3.以下為ajax提交驗(yàn)證碼到后臺(tái)校驗(yàn):
<script type="text/javascript"> $("#login_btn").click(function(){ var username = $.trim($("#username").val()); var password = $.trim($("#password").val()); var code = $('#veri').val(); if(username == ""){ NewAlert(2,"請(qǐng)輸入用戶名",null); shutdown(); return false; }else if(password == ""){ NewAlert(2,"請(qǐng)輸入密碼",null); shutdown(); return false; }else if(code==''){ NewAlert(2,"請(qǐng)輸入驗(yàn)證碼",null); return false; } //ajax去服務(wù)器端校驗(yàn) $.ajax({ url:"__CONTROLLER__/checkVerify", data:{'code':code}, dataType:'json', success:function(msg){ if(msg.flag==1){ var data= { username:username, password:password }; $.ajax({ type:"POST", url:"{:U('Login/Login')}", data:data, dataType:"json", success:function(msg){ if(msg.RespCode=='000'){ shutdown(); if(msg.org_code=='fcb'){ location.href="/Invest/index?biao_type=cwb"; }else{ location.href="{$Think.config.VIP_URL}/Individual/index"; } }else{ NewAlert(2,msg.RespDesc,null); return false; } }, error:function(){ shutdown(); }, beforeSend: function() { Loading(); }, }); }else{ $('#safecode').attr("src","/Login/Verify?"+Math.random()); NewAlert(2,"驗(yàn)證碼有誤,請(qǐng)重新輸入",null); code_ok = false; $('#verifyresult').html(msg.cont).css({'color':'red','font-size':'12px'}); } } }); }); </script>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Grass Wonder Build Guide | Uma Musume Pretty Derby
1 months ago
By Jack chen
Roblox: 99 Nights In The Forest - All Badges And How To Unlock Them
4 weeks ago
By DDD
Uma Musume Pretty Derby Banner Schedule (July 2025)
1 months ago
By Jack chen
RimWorld Odyssey Temperature Guide for Ships and Gravtech
3 weeks ago
By Jack chen
Windows Security is blank or not showing options
1 months ago
By 下次還敢

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)