二次开发-如何在PHPEMS引入第三方composer包-验证码实现!

gregwar/captcha包为例:php

https://packagist.org/packages/gregwar/captchasession

第一步:app

下载安装:composer require gregwar/captchacomposer

第二步:测试

在master.php类文件中引入ui

require_once __DIR__.'/../../vendor/autoload.php';
use Gregwar\Captcha\CaptchaBuilder;

在app类中定义属性,并在构造方法中把captcha对象赋值给该属性this

public $yzm;
$this->yzm =  new CaptchaBuilder;

 

第三步:url

测试地址:http://127.0.0.1/index.php?test-master-test1-yzm_yz&yzm=stwznspa

显示验证码和验证验证码3d

模板文件中:

<div class="col-xs-12">
    <h5 class="title">
        xxxxxxx
    </h5>
    <p>
        111111111111111111111


        <img src="{x2;$yzm}">
    </p>
</div>

 

<?php


class action extends app
{
    public function display()
    {
        $action = $this->ev->url(3);
        if (!method_exists($this, $action))
            $action = "index";
        $this->$action();
        exit;
    }

    private function index()
    {


        //这两步必须的,不然会不显示验证码
        $this->yzm->build();
        $_SESSION['phrase'] = $this->yzm->getPhrase(); //保存进session
        $yzm_img_url = $this->yzm->inline();

        $this->tpl->assign('yzm', $yzm_img_url);
        $this->tpl->display('test1');
    }


    //验证码验证码测试
    private function yzm_yz()
    {
        print_r($_SESSION);
        $userInput = $this->ev->get('yzm');
        //echo "<hr>";
        //echo $userInput . '-----';
        //  print_r($this->ev->get('yzm'));
       // var_dump($this->yzm->testPhrase($userInput));
        //这里这样验证始终有些问题,不建议
        //https://www.jianshu.com/p/8e4ac7852b5a
/*        if ($this->yzm->testPhrase($userInput)) {
            // instructions if user phrase is good
            echo 'ok';
        } else {
            // user phrase is wrong
            echo 'no';
        }*/

//        验证验证码
        if ($userInput === $_SESSION['phrase']) {
            echo 'ok';
        } else {
            echo 'no';
        }

    }


}
相关文章
相关标签/搜索