Home >Backend Development >PHP Tutorial >How to use thinkphp verification code
thinkThe Verify class in PHP can support the generation and verification functions of verification codes. This article will briefly explain how the verify class generates verification codes and implements the verification functions. I hope you will like it.
The verification code in thinkphp can be called directly, which is very convenient. Let’s take a look at the file named verify.class.php under the Think folder.
First we need to have a template , a file named xx.html was written under the view folder, with nothing 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(); }
If we want the verification code to be displayed, then we need to adjust the yzm method, which is represented in xx.html as
<p> <img src="__CONTROLLER__/yzm" id="img1"/> </p>
This way You can call out the verification code. After the verification code is called out, we need to use it. Then there will be an input form in xx.html and a verification button
<p> <input type="text" id="va"/> <input type="button" id="btn"/> </p>
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("验证成功"); } else { alert("验证失败"); } } }); }) </script>
Now let’s 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 that when we click on the picture, it will not change automatically. Then we have to add a click event to the picture, every time we click it Let’s walk the path again
$("#img1").click(function(){ //如果只是在除了ie浏览器的其他浏览器上运行 $(this).attr("src","__CONTROLLER__/yzm"); //如果要增加其兼容性的话,那么我们就要取一个随机数了 $a=Math.random()*10; $(this).attr("src","__CONTROLLER__/yzm/aa"+a); })
PHP method to implement simple numbersVerification code
PHP method to implement Verification codeGenerator
PHP realizes the method of generating various random verification codes
The above is the detailed content of How to use thinkphp verification code. For more information, please follow other related articles on the PHP Chinese website!