thinkphp3.2 验证码生成和点击刷新验证码
一、實例化生成驗證碼的類(該方法放到IndexController里面便于訪問)
[php]
/**? ?
?* 驗證碼生成
?*/ ?
public function verify_c(){ ?
??? $Verify = new \Think\Verify(); ?
??? $Verify->fontSize = 18; ?
??? $Verify->length?? = 4; ?
??? $Verify->useNoise = false; ?
??? $Verify->codeSet = '0123456789'; ?
??? $Verify->p_w_picpathW = 130; ?
??? $Verify->p_w_picpathH = 50; ?
??? //$Verify->expire = 600; ?
??? $Verify->entry(); ?
}
二、前臺需要生成驗證碼的圖片src屬性指向
[html]
?????? <input name="verify" width="50%" height="50" class="input_codebg" placeholder="驗證碼" type="text">???????????????? ?
? ?? ? <img width="30%" class="left15" height="50" alt="驗證碼" src="{:U('Home/Index/verify_c',array())}" title="點擊刷新" id="code_img">
</p>
三、寫完上面的后,頁面初始化的驗證碼就可以出現(xiàn)了,下面要寫的就是點擊驗證碼圖片后,刷新出新的驗證碼圖片(通過jquery修改圖片的src屬性來完成,請求的處理函數(shù)一樣,只是在請求后加一個隨機數(shù),區(qū)別上一張圖片的請求)
[js]
// 驗證碼生成 ?
?? ??? ?var captcha_img = $('#captcha-container').find('img') ?
?? ??? ?var verifyimg = captcha_img.attr("src"); ?
?? ??? ?captcha_img.attr('title', '點擊刷新'); ?
?? ??? ?captcha_img.click(function(){ ?
?? ??? ???? if( verifyimg.indexOf('?')>0){ ?
?? ??? ???????? $(this).attr("src", verifyimg+'&random='+Math.random()); ?
?? ??? ???? }else{ ?
?? ??? ???????? $(this).attr("src", verifyimg.replace(/\?.*$/,'')+'?'+Math.random()); ?
?? ??? ???? } ?
?? ??? ?});
四、校驗驗證碼輸入是否正確
a.在common目錄下的function.php里加入全局函數(shù)
/**
?* 驗證碼檢查
?*/ ?
function check_verify($code, $id = ""){ ?
??? $verify = new \Think\Verify(); ?
??? return $verify->check($code, $id); ?
}
b.在表單提交的controller對應的處理方法里添加檢查代碼
?? // 檢查驗證碼 ?
?? $verify = I('param.verify','');?
??? if(!check_verify($verify)){ ?
???????? $this->error("親,驗證碼輸錯了哦!",$this->site_url,9); ?
???? }
轉(zhuǎn)載于:https://blog.51cto.com/6781547/1809620
總結(jié)
以上是生活随笔為你收集整理的thinkphp3.2 验证码生成和点击刷新验证码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux安装 Android Stud
- 下一篇: ToolStripStatusLabel