Home>

The verify class in thinkphp can support the generation and verification of verification codes.This article will give you a brief explanation of the verification class to generate verification code and the implementation of the verification function.Hope everyone likes it.

The verification code in thinkphp can be called directly,Very convenient,Let's take a look at a file named verify.class.php under the think folder

First we have a template,A file named xx.html was written under the view folder, and nothing was written in it. At this time, we need to display the verification code in this file.

function xx ()
{
  $this->show ();
}
function yzm ()
{
  $yzm=new \ think \ verify ();
  $yzm->entry ();
}

Want the verification code to show up,Then we need to adjust the yzm method, which is expressed in xx.html as

<div>
  <img src="__ controller __/yzm" />
</div>

In this way, the verification code can be called out. After the verification code is called out, we need to use it.Then there will be an input form in xx.html and a button for validation

<div>
  <input type="text" />
  <input type="button" />
</div>

The js part I wrote below uses jquery, so the jquery package must be introduced before.

<script type="text/javascript">
  $("#btn"). click (function () {
    var va=$("#va"). val ();
    $.ajax ({
     url:"__ controller __/yz",     data:{va:va},     type:"post",     datatype:"text",     success:function (data) {
        if (data)
        {
           alert ("Authentication succeeded");
        }
       else
          {
             alert ("verification failed");
          }
     }
    });
  })
</script>

Now we go to write the yz method

function yz ()
{
  $yzm=new \ think \ verify ();
  $va=$_ post ["va"];
   $aa=$yzm->check ($va);
  $this->ajaxreturn ($aa, "eval");
}

After writing this,We will find a problem is that when you click on the picture, it will not change automatically.Then we need to add a click event to the picture,Every time you click, let the path go again

$("#img1"). click (function () {
//if it only runs on browsers other than Internet Explorer
 $(this) .attr ("src", "__ controller __/yzm");
//If i want to increase its compatibility,Then we have to take a random number
$a=math.random () * 10;
 $(this) .attr ("src", "__ controller __/yzm/aa" + a);
})
php
  • Previous Analyze some methods in thinkphp framework
  • Next Detailed explanation of json interactive use in SpringMVC tutorial