• tp5 composer在packagist引入验证码


    1.打开官网https://packagist.org/

    2.输入验证码单词captcha,查看项目列表,并选择喜欢的,比如选择第一个

    3.用composer导入到项目中,速度慢先开启中国镜像

    composer config -g repo.packagist composer https://packagist.phpcomposer.com

    4.复制该项目的composer指令

    composer require gregwar/captcha                

    5.此时在项目的vendor下可以看到

    6.在控制器Login.php的开的头加入

    use GregwarCaptchaCaptchaBuilder;

    7.在控制器Login.php书写验证码方法

    <?php

    namespace appindexcontroller;

    use thinkController;
    use GregwarCaptchaCaptchaBuilder;

    class Login extends Controller
    {

    public function login()
    {
    header('Content-type: image/jpeg');
    $builder = new CaptchaBuilder();


    $builder->build($width = 100, $height = 40, $font = null);
    //获取验证码的内容
    $phrase = $builder->getPhrase();

    //把内容存入session
    Session('milkcaptcha',$phrase);
    //生成图片
    header('Cache-Control: no-cache, must-revalidate');
    header('Content-Type: image/jpeg');

    $builder->save('out.jpg');
    return $this->fetch('login/login');

    }

    }

    注意:加上exit(0) 并且exit(0)前面不能有任何输出

    <!DOCTYPE html>
    <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>Document</title>
    </head>
    <body>

    <input type="text" name="captcha" class="form-control" style="300px;">
    <a onclick="javascript:re_captcha();"><img src="../../images/out.jpg" alt="验证码" title="刷新图片" width="100" height="40" id="c2c98f0de5a04167a9e427d883690ff6" border="0"></a>
    </body>
    </html>
    <script>
    function re_captcha() {
    $url="";
    document.getElementById('c2c98f0de5a04167a9e427d883690ff6').src=$url;
    }
    </script>

    解析:

    保存验证码图片到public下

    <?php

    $builder->save('out.jpg');

    或者直接输出图片到网页:

    <?php

    header('Content-type: image/jpeg');

    $builder->output();

    或者生成内联图片

    <img src="<?php echo $builder->inline(); ?>"/>

    验证验证码是否正确:

    public function verify()
    {
    $request = Request::instance();
    $builder = new CaptchaBuilder();
    $userInput = $request->post('captcha');
    dump($userInput);
    if($builder->testPhrase($userInput)){
    return '正确';
    }else{
    return '错误';
    }
    exit;

    }

  • 相关阅读:
    Windows环境下 配置memcached (php)
    谈谈我是怎么学习PHP的(一)
    重编译Linux命令源代码
    php面向对象学习
    Windows文件系统漏洞
    十分钟能学会的框架,MVC+20个常用函数
    linux系统安装软件方法大全
    oracle基本操作
    MySQL的limit查询优化
    C# winform 可视化操作 Excel文件并读取数据
  • 原文地址:https://www.cnblogs.com/ymdphp/p/10937856.html
Copyright © 2020-2023  润新知