二次開發-如何在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';
        }

    }


}
相關文章
相關標籤/搜索