Mac_ThinkPHP_验证码的使用

如果验证码显示不出来 出来的是一个裂开的小图标 那说明 很有可能是Mac自带

的PHP没有 gd下面freetype这个扩展库

建议使用 MAMP这个Mac端的PHP集成环境 相当于Window端的 PHPStudy 还是很方便很好用的

MAMP下载地址👇

链接:https://pan.baidu.com/s/174hjy9M6aDZGcDDuD06UMw  密码:41r5

 

实现验证码功能

LoginValidate 验证器的代码

<?php

namespace app\index\validate;

use think\Validate;

class LoginValidate extends Validate
{
    /**
     * 定义验证规则
     * 格式:'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */	
	protected $rule = [
	    'code' => 'require|captcha|token'
    ];
    
    /**
     * 定义错误信息
     * 格式:'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */	
    protected $message = [
        'code.require' => '你想干什么,验证码都不录入',
        "code.captcha" => "您输入的验证码有错误"
    ];
}

 

Login控制器的代码

<?php

namespace app\index\controller;

use app\index\validate\LoginValidate;
use think\captcha\Captcha;
use think\Controller;
use think\Request;
use verify\Verify;

class Login extends Controller {

    // pathinfo模式
    /*protected $middleware = [
        // 中间件的类名称
        'CheckLogin'1
        # 中间件传值
        #'CheckLogin:user'
    ];*/

    // 登录界面
    public function index() {
        //dump(phpinfo());
        return view('index@login/index');
    }

    // 登录处理
    public function login(Request $request) {
        // 验证器去验证验证码 推荐方式
        $ret = $this->validate($request->post(), LoginValidate::class);
        if (true !== $ret) {
            return $this->error($ret);
        }

        // 独立去验证验证码
/*        $code = $request->post();
        // 如果验证不通过,则返回false
        if (!captcha_check($code)) {
            return $this->error('验证不正确');
        }*/

        dump($request->post());
    }
}

 

view -> index.html的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
    <link rel="stylesheet" href="/static/index/css/bootstrap.css"/>
    <link rel="stylesheet" href="/static/index/css/bootstrap-theme.css"/>
    <style>
        #vcode {
            cursor: pointer;
        }
    </style>
</head>
<body>

<br>
<div class="container">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">用户登录</h3>
        </div>
        <div class="panel-body">
            <form class="form-horizontal" method="post" action="{:url('index/login/login')}">
                <!--csrf验证-->
                {:token()}
                <div class="form-group">
                    <label class="col-sm-2 control-label">账号:</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="title">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">密码:</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="desn">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">验证码:</label>
                    <div class="col-sm-5">
                        <input type="text" class="form-control" name="code">
                    </div>
                    <div class="col-sm-5">
                        <img src="{:captcha_src()}" id="vcode">
<!--                        <div>{:captcha_img()}</div>-->
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-success">用户登录</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


<script src="/static/js/jquery.min.js"></script>
<script src="/static/index/js/bootstrap.js"></script>
<script>

    // 点击更新验证码图片
    $('#vcode').click(function () {
        let src = $(this).attr('src') + '?vt=' + Math.random();
        //alert(src);
        $(this).attr('src',src);
    })
</script>
</body>
</html>

 

我这里使用了 bootstrap框架实现验证码登陆功能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值