1、下载
composer require topthink/think-captcha 1.*
// composer 下载
//过程
D:\PHP\phpstudy_pro\WWW\1906A\pyg>composer require topthink/think-captcha 1.*
./composer.json has been updated
Running composer update topthink/think-captcha
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking topthink/think-captcha (v1.0.8)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Downloading topthink/think-captcha (v1.0.8)
- Installing topthink/think-captcha (v1.0.8): Extracting archive
Generating autoload files
//下载完成后
2.验证码包的位置:
3. 修改view/login/login.html 验证码图片img标签src属性
<!DOCTYPE html>
{__NOLAYOUT__}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>登录</title>
<link rel="stylesheet" href="__STATIC__/admin/css/login.css" rel="external nofollow" >
</head>
<body>
<form action="/goods/login/save" method="post">
<div class="login">
<div class="center">
<h1>Login</h1>
<div class="inputLi">
<strong>账户</strong>
<input type="text" placeholder="账户" name="account">
</div>
<div class="inputLi">
<strong>密码</strong>
<input type="password" placeholder="密码" name="password">
</div>
<div class="inputLi">
<strong>验证码</strong>
<input type="text" placeholder="" name="cord">
<img src="{:captcha_src()}" name="img" onclick="this.src='{:captcha_src()}'"
>
</div>
<div class="inputLi">
<button type="submit">登录</button>
</div>
</div>
</div>
</form>
</body>
</html>
4.控制器验证登录,记录session
public function save(Request $request)
{
//
$params = $request->param();
//验证参数、非空
$rule = [
'account' => 'require',
'password' => 'require',
'cord' => 'require',
];
$tips = [
'account.require' => '账号不可以为空',
'password.require' => '密码不可以为空',
'cord.require' => '验证码不可以为空',
];
$validate = new Validate($rule, $tips);
$result = $validate->check($params);
if (!$result) {
$this->error($validate->getError());
}
//数据库验证参数
$data = GoodModel::login($params);
//验证账号
if ($params['account'] != $data['name']) {
$this->error('账号错误', '/goods/login/create');
}
//验证密码
if (md5($params['password']) != md5($data['password'])) {
$this->error('密码错误', '/goods/login/create');
}
//验证码进行验证
if (!captcha_check($params['cord'])) {
$this->error('验证码输入错误', '/goods/login/create');
};
if ($data){
//记录session
session('name',$data['name']);
$this->success('登录成功', '/goods/goods/index');
} }