验证码的使用以及相关配置

<?php
/**
 * Created by PhpStorm.
 * User: admin.Li
 * Date: 2018/5/16
 * Time: 17:17
 */

namespace app\index\controller;


use think\Controller;

class Yzm extends Controller
{
    public function index(){
        return $this->fetch();
    }

    //验证码的处理方法
    public function check(){
        $captcha=input('post.code');

        //判断验证码是否正确
        if(!captcha_check($captcha)){
            //验证失败
            echo 'error';
        }else{
            echo 'ok';
        }
    }
}
Yzm.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>验证码测试</title>
</head>
<body>
<form action="{:url('check')}" method="post">
    <p>
        验证码:<!--{:captcha_img()}-->
        <img src="{:captcha_src()}" alt="captcha" />
    </p>
    <p>
        请输入验证码:
        <input type="text" name="code" id="">
    </p>
    <p>
        <input type="submit" value="提交">
    </p>
</form>
</body>
</html>
yzm_index.html

验证码相关配置

//具体的配置参考扩展类Captcha.php

'captcha'               =>[
        //验证码的字符集和
        'codeSet'=>'123',
        // 使用中文验证码
        'useZh'    => true,
        // 中文验证码字符串
        'zhSet'    => '大家好才是真的好',
        // 验证码位数
        'length'   => 4,

    ],
config.php中设置

猜你喜欢

转载自www.cnblogs.com/lichao666888/p/9047468.html