Joke Collection Website - Public benefit messages - How to solve common problems with verification codes in thinkphp5

How to solve common problems with verification codes in thinkphp5

Generate captcha extension under the project directory (composer needs to be installed to install)

composer require topthink/think-captcha

2 After the installation is completed, the above will be displayed. Table of contents.

After the captcha extension is installed, you can proceed to the next step

Configure the comfig.php file: add an extension under comfig.php

//Verification code

'captcha'=gt; [

//Character set

'codeset'=gt;'23456780qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM',

//Font size

'fontSize' =gt; 18,

//Whether to use confusion curve

'useCurve' =gt; true,

//Picture Height

'imageH' =gt; 40,

//Image width

'imageW' =gt; 130,

/ /number of digits

'length' =gt; 4,

//Whether to reset after successful verification

'reset' =gt; true,

p>

],

3. Refresh verification code function? The src path can be used here, which comes with the framework, or you can directly access the img method

lt; img id=" captcha_img" src="{:captcha_src()}" alt="Verification code" onclick="refreshVerify()"gt;lt;a

href="javascript:refreshVerify()"gt;Click to refresh lt;/agt;

Add refresh event in js "script" tag part

function refreshVerify()

{

var ts = Date.parse(new Date() )/1000;

console.log(ts);

$('#captcha_img').attr('src',' /captcha?id=' ts);

}

4. Use the TP verification method in the controller

In your login control Add it to the browser

4.1 Introduce use think\captcha\Captcha in the header;

//This method introduces the img image? The width and height can be directly controlled by CSS using img!

public function img() {

$captcha = new Captcha();

return $captcha-gt; entry();

}

// Check whether the entered verification code is correct. $code is the verification code string entered by the user. $id is a multiple verification code identifier

function check_verify($code , $id = ''){

$captcha = new Captcha();

return $captcha-gt;check($code, $id);

}The following verification of this method will be used

In the method of sending the form verification code value, add post submission. If GET is submitted, change post to get? The name of the form verification code is code

$request=request();

if ($request-gt; isPost()){

if($request-g

t; post('code')){

if($this-gt; check_verify($request-gt; post('code'))){

$message= 'Verification successful';

}else{

$message= 'Verification error';

}

}else{

$message= 'No verification code entered';

}

}

Follow the above and there will be no problem