thinkphp Verification Code implementation.

Source: Internet
Author: User

As a programmer of my big celestial, if do not point thinkphp frame does a little justify. But it doesn't matter, it's a simple PHP framework. Today, we introduce the function of thinkphp how to implement verification code.

Thinkphp comes with verification code function, we call directly on the line. There have been several official versions of thinkphp, and the latest version is thinkphp5.0. Today I will introduce you to the 5 version and the 3 version.

Version of THINKPHP3:

The first step: Create an output Captcha method in our corresponding controller

Public Function Verify () {    $Verify = new \think\controller\verify ();    $Verify->fontsize = 20; Font size    $Verify->length = 6;//How many characters    $Verify->usenoise = false;//whether to add a miscellaneous point    $Verify->imagew = 233; Verification code Width    $Verify->imageh = 50;//Verification Code height    $Verify->entry ();}

Step two: Output our verification code on the page

class= "Verifycode" name= "admin_verify" title= "click Refresh Captcha" >

Complete the above operation. Next, we begin to implement additional functionality.

The first click of the verification code to implement the refresh (in fact, using an Ajax method to reload the image output): In our JS write the following method.

var verifyimg = $ (".  Verifycode "); var verifypath = verifyimg. attr ("src"); verifyimg. Click (function  () {if (Verifypath.indexof ("?") >0) {$ (this). attr ("src", verifypath+ ' &random= ' +math.  Random ());} Else {$ (this). attr ("src", Verifypath.replace (/\?). *$/, ') + '? ' +math. random ());})}

The second is the background verification of our verification code correctness: first create a public validation function (the public configuration function file is under common, can be defined in the project's public directory can also be in the module's functional directory), and we accept the user information submitted to the place to do the validation and error feedback to the user.

Public validation functions (recommended in the project's Common directory for multiple module calls)
Public functions can be used directly in the system
function
check_verify ($code$id = "") { $verifynew \think\ Verify (); return $verify->check ($code$id);}

// receive the input verification code value of the page $data [' admin_verify '] = I (' Post.  Admin_verify '); if (Check_verify ($data[' admin_verify '])) {    $this->error ("Incorrect Captcha", U ("Login"), 3 );}

Ah, manual valve manual valve manual valve

thinkphp Verification Code implementation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.