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{ p>
$message= 'No verification code entered';
}
}
Follow the above and there will be no problem
- Related articles
- Is there a better way to get people to pay back the money?
- Why is there no sound on WeChat? Wechat video, what if the voice has no sound and prompt tone? Why is there no sound and vibration prompt in the message from WeChat?
- What would you do if you were put in charge of the reception?
- I received a message at 106980095 188 that someone had transferred 100 yuan to my China bank card through Alipay, and the suffix was xxxx. What's going on here? The suffix xxx is not mine.
- What are the consequences of spending two days overdue?
- Is there a charge for sending short messages by Fetion?
- How to Look at Blacklist Interception Records
- All free services and introductions of Beijing Mobile M-Zone. Please help me, experts, thanks for the high score!
- How to view the content of sent SMS?
- How to Restore and Export Deleted SMS to Computer: Apple