一、首先安装Composer,已经安装就跳过
不会的看这里:https://blog.csdn.net/haibo0668/article/details/80435529
二、安装think-captcha验证码包
1、打开电脑CMD命令窗口,然后进入项目\vendor\topthink目录下,就可以运行命令
进入E盘方法:
composer require topthink/think-captcha
登录验证验证码
<?php
namespace app\admin\controller;
use think\Controller;
use think\Validate;
use think\Request;
use think\Db;
use think\Session;
class Login extends controller
{
public function index()
{
return $this -> fetch();
}
public function login()
{
$username=input('username');
$password1=input('password');
if(!$username || !$password1){
$this->error('错误:用户名、密码不能为空');
}
$cms_password = cms_password;
if(request()->isPost()){
$this->check(input('code'));
$password=md5($password1.$cms_password);
if($username){//验证用户名
$rsu= Db::name('admin')->where('name',$username)->find();
$countu = count($rsu);
if(!$countu){
$s1=1;//=1失败
}else{
$s1=9;//=9成功
}
}
if($s1==1){
$this->error('用户不存在!');
}
if($username && $password1){//验证密码
$rsp= Db::name('admin')->where('name',$username)->where('password',$password)->find();
$countp = count($rsp);
if(!$countp){
$s2=1;
}else{
$s2=9;
$id=$rsp['id'];
$group_id=$rsp['group_id'];//权限分组ID
}
}
if($s2==1){
$this->error('密码错误!');
}
if($s1==9 && $s2==9){
session('id', $id);
session('username', $username);
session('group_id', $group_id);
$this->success('登录成功!',url('/admin/index'));
}
return;
}
return $this -> fetch();
}
// 验证码检测
public function check($code='')
{
if (!captcha_check($code)) {
$this->error('验证码错误');
} else {
return true;
}
}
}
重点:
1、调用检查验证码是否正确
$this->check(input('code'));
2、验证码检测
// 验证码检测
public function check($code='')
{
if (!captcha_check($code)) {
$this->error('验证码错误');
} else {
return true;
}
}