tp5 of thinkPHP5 Framework captcha Verification Code Configuration and Verification Operation Example

  • 2021-12-11 17:28:59
  • OfStack

This paper describes the configuration and verification operation of tp5 (thinkPHP5 framework) captcha verification code. Share it for your reference, as follows:

1-Installation


composer require topthink/think-captcha 1.* -vvv

2-Configuration file


'captcha' => [    //  Verification code character set 3.
  'codeSet' => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',     //  Verification code font size (px)5.
  'fontSize' => 28,     //  Whether to draw confusion curve 7.
  'useCurve' => false,     //  Verification code picture height 9.
  'imageH'  => 50,    //  Verification code picture width 11.
  'imageW'  => 200,     //  Verification code number 13.
  'length'  => 4,     //  Reset after successful verification     15.
  'reset'  => true
  ],

3-html code


<div class="yzm">
  <img src="{:captcha_src()}" class="verify" onclick="resVerify()" alt=" Verification code " />
<a href="javascript:resVerify();" rel="external nofollow"  > Can't see clearly, change 1 Zhang </a>
</div>

Or


<img src="{:captcha_src()}" class="verify" 
onclick="javascript:this.src='{:captcha_src()}?rand='+Math.random()" >

4-Authentication at Logon Time of Controller


$captcha=input("captcha");
 // Validation 
if (!captcha_check($captcha)){
  exit(json_encode(array('status'=>0,'msg'=>' Verification code error ')));
}

Or


// Verification code verification code 
$captcha = new Captcha();
if (!$captcha->check(input("captcha"))){
  return $this->error(" Verification code error ");
}

Additional features and plug-ins can be found at: https://www.kancloud.cn/he_he/thinkphp5/787173

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: