极验滑动验证码

文档地址https://docs.geetest.com/install/deploy/server/php/;

下载SDK

将GeetestLib.php放置vendor\jiyan目录中;

新建GeetestCheck.php

<?php
namespace app\index\logic;
use think\Loader;
/**
 * Description of Installer
 * 滑动验证码
 * @author Administrator
 */
class GeetestCheck extends \think\Controller
{
   private $captcha_id = '******';
   private $private_key = '******';
    /**
     * 配置用户信息
     * @param $user_id
     * @return array
     */
    public function getConfig($user_id) {
       $config = [
           "user_id" => $user_id ? $user_id : 'wsg', # 网站用户id
           "client_type" => "web", #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式
           "ip_address" => request()->ip(), # 请在此处传输用户请求验证时所携带的IP
       ];
       return $config;
   }

    /**
     * 实例化验证码类
     */
   public function geetestLib() {
       loader::import('jiyan.GeetestLib',VENDOR_PATH, EXT);
       $geetestLib = new \GeetestLib($this->captcha_id, $this->private_key);
       return $geetestLib;
   }

    /**
     * 获取验证码
     * @return mixed
     */
   public function getGeetestResponse() {
       $data = $this->getConfig('tianlun');
       $GtSdk = $this->geetestLib();
       $status = $GtSdk->pre_process($data, 1);
       \think\Session::set('gtserver',$status);
       \think\Session::set('user_id',$data['user_id']);
       return $GtSdk->get_response_str();
   }

    /**
     * 验证码检验 二次验证
     * @return bool|void
     */
    public function checkCode() {
        $map = $this->request->post();
        $GtSdk = $this->geetestLib();
        $data = $this->getConfig(session('user_id'));
        if (session('gtserver') == 1) {   //服务器正常
            $result = $GtSdk->success_validate($map['geetest_challenge'], $map['geetest_validate'], $map['geetest_seccode'], $data);
        }else{
            //服务器宕机,走failback模式
            $result = $GtSdk->fail_validate($map['geetest_challenge'],$map['geetest_validate'],$map['geetest_seccode']);
        }
        if ($result) {
            return true;
        } else{
            return $this->error(config('msg.663'));//验证失败
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/qq_20521363/article/details/81783012