thinkphp5 整合geetest極驗滑動驗證碼

專業的事給專業的作,驗證碼第三方化仍是比較爽的php

    

官網:http://www.geetest.com/html

一:註冊獲取key
    註冊->建立應用->獲取id和keyjquery

二:配置thinkphp

    /conf/config.php(配置文件提出到根目錄下conf)json

    

三:導入sdkapp

    /extend/geetest/Geetest異步

    

四:生成驗證樣式函數

<div class="box">
    <h4>
        <a target="_blank" href="#">thinkphp5整合geetest極驗滑動驗證碼</a>
    </h4>
    <form id="geetest" action="{:url('index/User/login')}" method="post">
        帳號:<input type="text" name="username" value="admin">
        密碼:<input type="text" name="password" value="111111">
        <input type="button" value="異步驗證登陸" onclick="verify()">
        <input type="submit" value="post提交登陸">
        <div id="captcha"></div>
    </form>
</div>
<script src="/Public/statics/js/jquery-1.10.2.min.js"></script>
<script src="http://static.geetest.com/static/tools/gt.js"></script>
<script>
    var handler = function (captchaObj) {
        // 將驗證碼加到id爲captcha的元素裏
        captchaObj.appendTo("#captcha");
     };
    // 獲取驗證碼
    $.get("{:url('index/User/captcha')}", function(data) {
        // 使用initGeetest接口
        // 參數1:配置參數,與建立Geetest實例時接受的參數一致
        // 參數2:回調,回調的第一個參數驗證碼對象,以後能夠使用它作appendTo之類的事件
        initGeetest({
            gt: data.gt,
            challenge: data.challenge,
            product: "float", // 產品形式
            offline: !data.success
        }, handler);
    },'json');
    // 檢測驗證碼
    function verify(){
        // 組合驗證須要用的數據
        var data = {
            geetest_challenge: $('.geetest_challenge').val(),
            geetest_validate: $('.geetest_validate').val(),
            geetest_seccode: $('.geetest_seccode').val()
        }
        // 驗證是否經過
        $.post("{:url('index/User/gtAsyncVerify')}", data, function(response) {
            if (response == 1) {
                alert('驗證成功');
            }else{
                alert('驗證失敗');
            }
        });
    }
</script>

五:驗證函數thinkphp5

    /conf/extra/function.phppost

<?php
use geetest\Geetest;
use think\Session;
/**
 * geetest檢測驗證碼
 * @param $data
 * @return bool
 */
function gtVerify($data){
    $geetest_id = config('geetest.id');
    $geetest_key = config('geetest.key');

    $geetest = new Geetest($geetest_id, $geetest_key);
    $user_id = Session::get('geetest.user_id');
    if (Session::get('geetest.gtserver') == 1) {
        $result = $geetest->success_validate($data['geetest_challenge'], $data['geetest_validate'], $data['geetest_seccode'], $user_id);
        if ($result) {
            return true;
        } else{
            return false;
        }
    }else{
        if ($geetest->fail_validate($data['geetest_challenge'], $data['geetest_validate'], $data['geetest_seccode'])) {
            return true;
        }else{
            return false;
        }
    }
}

六:php 生成驗證碼 並 驗證

/**
     * geetest生成驗證碼
     */
    public function captcha(){
        $geetest_id = config('geetest.id');
        $geetest_key = config('geetest.key');

        $geetest = new Geetest($geetest_id, $geetest_key);
        $user_id = [uniqid() . rand(1000, 9999)];
        $status = $geetest->pre_process($user_id);
        Session::set('geetest', ['gtserver' => $status, 'user_id' => $user_id]);
        echo $geetest->get_response_str();
    }
    
    /**
     * 登陸
     * @return mixed
     */
    public function login()
    {
        if(request()->isPost()){
            $data = input('post.');
            if(!gtVerify($data)){
                $this->error('驗證碼錯誤');
            }
            $res = self::$event->login($data);
            if($res['status'] == 1){
                return $this->success($res['msg']);
            }else{
                return $this->error($res['msg']);
            }
        }else{
            return $this->fetch();
        }
    }
    
    /**
     * geetest異步驗證
     */
    public function verify(){
        $data = input('post.');
        echo intval(gtVerify($data));
    }
相關文章
相關標籤/搜索