English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Comprehensive Analysis of thinkphp's Built-in Captcha

Front-end page:

<div style="position:absolute;z-index:3;top:160px;left:180px;">
<img style="cursor:pointer; " src="{:U('Verify')}" onclick="this.src=this.src+'?'+Math.random()" id="safecode" style="height:50px;width:70%;"/>
</div> 
//Captcha judgment
public function Verify(){
ob_clean();
//Display captcha
$cfg=array(
'codeSet' => '0123456789', // Captcha character set
'imageH' => 25, // Captcha image height
'imageW' => 80, // Captcha image width
'length' => 4, // 
'fontttf' => '4.ttf', // Captcha font, if not set, get randomly
'fontSize' => 10, // Captcha font size (px)
'useNoise' => false, // Whether to add noise
'useCurve' => false, // Whether to draw a confusion curve
'bg' => array(226,229,236) //Background color
);
$very=new \Think\Verify($cfg);
$very->entry();
} 
//The client uses AJAX to validate the captcha
public function checkVerify(){
$code = I('get.code');
$very = new \Think\Verify();
$key = $this->auth_my_code($very,$very->seKey);
// The captcha cannot be empty
$secode = session($key);
//Encrypt $code and compare for verification
if($this->auth_my_code($very,strtoupper($code)) == $secode['verify_code']) {
echo json_encode(array('flag'=>1,'cont'=>'Verification code correct'));
}
echo json_encode(array('flag'=>2,'cont'=>'Verification code error');
}
}
private function auth_my_code($vry,$str){
$key = substr(md5($vry->seKey), 5, 8);
$str = substr(md5($str), 8, 10);
return md5($key . $str);
}

If the verification code is entered incorrectly and cannot be refreshed automatically after submission, the code can be changed as follows:

location.href="/Login/Login";This will refresh the entire page, and the values of the submitted form may be lost, which greatly affects the user experience.

2.If the verification code is entered incorrectly, the verification code will be refreshed automatically after submission.

else{
$('#safecode').attr("src","/Login/Verify?"+Math.random());
NewAlert(2,"Verification code is incorrect, please re-enter",null);
code_ok = false;
$('#verifyresult').html(msg.cont).css({'color':'red','font-size':'12px'}); 
}

3.The following is the ajax submission of the verification code to the backend for verification:

<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,"Please enter the username",null);
shutdown();
return false;
}else if(password == ""){
NewAlert(2,"Please enter the password",null);
shutdown();
return false;
}else if(code==''){
NewAlert(2,"Please enter the verification code",null);
return false;
}
//ajax to verify on the server side
$.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";
}
location.href="{$Think.config.VIP_URL}/Individual/index";
}
}
NewAlert(2,msg.RespDesc,null);
return false;
}
,
error:function(){
shutdown();
,
beforeSend: function() {
Loading();
,
});
}
$('#safecode').attr("src","/Login/Verify?"+Math.random());
NewAlert(2,"Verification code is incorrect, please re-enter",null);
code_ok = false;
$('#verifyresult').html(msg.cont).css({'color':'red','font-size':'12px'}); 
}
}
});
}); 
</script>

The above is the comprehensive analysis of the built-in captcha of thinkphp introduced by the editor, hoping it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. Thank you very much for your support of the Yell Tutorial website!

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not bear relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like