搜索
首页php教程php手册ThinkPHP5如何实现验证码的效果

ThinkPHP5如何实现验证码的效果

Jun 13, 2016 pm 12:20 PM
thinkphp5验证码

这篇文章主要和大家分享了ThinkPHP5的验证码的实现方法,有一定的参考价值,感兴趣的朋友可以看看。

 1、首先确认文件夹\vendor\topthink\think-captcha存在

abggg.jpg

2、显示验证码的方法,我这里是写在:\application\admin\controller\Login.php

//显示验证码
    public function show_captcha(){
        $captcha = new \think\captcha\Captcha();
        $captcha->imageW=121;
        $captcha->imageH = 32;  //图片高
        $captcha->fontSize =14;  //字体大小
        $captcha->length   = 4;  //字符数
        $captcha->fontttf = '5.ttf';  //字体
        $captcha->expire = 30;  //有效期
        $captcha->useNoise = false;  //不添加杂点
        return $captcha->entry();
    }

3、模板文件\application\admin\view\Login\index.html中这样引用验证码

<form action="/login/login_post" method="post">
<input type="text" class="input" name="captcha" placeholder="填写右侧的验证码" data-validate="required:请填写右侧的验证码"  style="width: 200px;"/>
                                <img src="/login/show_captcha" alt="" width="121" height="32" class="passcode" onclick="this.src=this.src+&#39;?&#39;"/>

4、同控制器中login_post方法

//提交
    public function login_post(){
        $code=input('post.captcha');
        $captcha = new \think\captcha\Captcha();
        $result=$captcha->check($code);
        if($result===false){
            echo '验证码错误';exit;
        }
        echo '验证码正确,继续';exit;
    }

这样就完成了thinkphp5中的验证码操作,有图有真相

 abvvvaaa.jpg

【相关教程推荐】

1. php编程从入门到精通全套视频教程
2. php从入门到精通 
3. bootstrap教程

声明
本文转载于:CSDN博客。如有侵权,请联系admin@php.cn删除

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具