Thinkphp5 & 5.1 example of generating, using and clicking refresh function of verification code

Time:2021-7-24

This paper describes the generation, use and click refresh of verification code in thinkphp5 & 5.1. Share with you for your reference, as follows:

Verification code is now a necessary element in many links such as user login and payment. Thinkphp5 & 5.1 provides us with the generation method of verification code, which is also very simple. Write a complete method of verification code here for your reference.

The foreground user needs authentication code to log in. First, use composer to install the think captcha Expansion Pack:


composer require topthink/think-captcha

Where the verification code needs to be displayed:

<tr>
 < th > please enter the verification code: < / th >
 <td>
  <input type="text" name="captcha"/>
  < img SRC = "{: URL ('index / login / verify ')}" ALT = "verification code loading" / >
 </td>
</tr>

Where verify is the method of generating verification code:

public function verify()
{
  $config =  [
    //Verification code font size
    'fontSize'  =>  15,
    //Verification code number
    'length'   =>  3,
    //Close verification code clutter
    'useNoise'  =>  false,
  ];
  $captcha = new \think\captcha\Captcha($config);
  return $captcha->entry();
}

Where $config is the configuration item of verification code. Please refer to the manual for specific configuration contents.

Use ajax or jQuery validate to verify the verification code, or submit it for verification later:


public function checkcapcha()
{
  $captcha = new \think\captcha\Captcha();
  if( !$captcha->check(input('post.captcha')))
  {
   return false;
  }else{
    return true;
  }
}

In order to refresh the click verification code, you need to write JS code:


$("#captcha").click(function(event) {
  this.src = "{:url('index/login/verify')}?"+Math.random();
});

In this way, the verification function of the verification code is realized. Is it very simple?

Readers interested in more ThinkPHP related content can view the special topics of this site: ThinkPHP introductory tutorial, ThinkPHP template operation skills summary, ThinkPHP common methods summary, CodeIgniter introductory tutorial, CI (CodeIgniter) framework advanced tutorial, Zend framework introductory tutorial and PHP template technology summary.

I hope this article will be helpful to your PHP Programming Based on ThinkPHP framework.

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]