ThinkPHP使用极验验证

羽化
2016-05-24 / 0 评论 / 589 阅读 / 正在检测是否收录...

1、极验验证官网http://www.geetest.com/,与以往传统验证码不同的是,极验通过分析用户完成拼图过程中的行为特征,通过数据分析来判断是人还是机器。用户不必面对眼花缭乱的英文字符或汉字,整个验证过程变的像游戏一样有趣。

如图:
4445477-8362bee3ca48aab2.png

2、首先先下载好极验的php的压缩包,链接: http://pan.baidu.com/s/1bpHRvsV 密码: 3b4m

3、复制gt-php-sdk-master\gt-php-sdk-master\lib\class.geetestlib.php文件到ThinkPHP\Extend\Vendor\Geetestlib下,没有文件夹则需要新建文件夹

4、极验主要分为两步,第一次请求是请求得到拖拽图片,第二次请求是验证拖拽的正确性

5、前端HTML中

<div id="float-captcha" style="margin-top: 40px;"></div>
 
$.ajax({
// 获取id,challenge,success(是否启用failback)
url: "/index.php?m=Index&a=StartCaptchaServlet&t=" + (new Date()).getTime(), 
type: "get",
dataType: "json",
success: function (data) {
    data =  $.parseJSON(data);
    initGeetest({
        gt: data.gt,
        challenge: data.challenge,
        product: "float", 
        offline: !data.success 
    }, handlerPopup);
}
});
 
var handlerPopup = function (captchaObj) {
 
$("#loginAction").click(function () {
    var validate = captchaObj.getValidate();
    if (!validate) {
        shwoDialog(true,"请先完成验证");
 
        return;
    }
 
    if(checkLogin()){
        // 二次验证所需的三个值
 
    $.ajax({
        type: 'POST',
        url: '/index.php?m=Index&a=btnLogin',
        data: {
            loginName: $("#loginName").val(),
            loginPassword:md5($("#loginPassword").val()).substring(8,24),
            geetest_challenge: validate.geetest_challenge,
            geetest_validate: validate.geetest_validate,
            geetest_seccode: validate.geetest_seccode
        },
        dataType: 'json',
        //timeout: 3000,
        success: function(rs){
            if(rs.status == 1){
                window.location = "/index.php?m=Index&a=index";
            }else{
                shwoDialog(true,rs.info);
            }
 
        },
        error: function(xhr, type){
            shwoDialog(true,"访问不到服务器");
        }
    });
    }
 
});
 
captchaObj.bindOn("#loginAction");
captchaObj.appendTo("#float-captcha");
 
};

6、服务器端

/**
     * 极验初始验证
     */
    public function StartCaptchaServlet () {
        vendor('Geetestlib.class#geetestlib');
        $GtSdk = new GeetestLib(C("CAPTCHA_ID"), C("PRIVATE_KEY"));
        session_start();
        $user_id = "test";
        $status = $GtSdk->pre_process($user_id);
        $_SESSION['gtserver'] = $status;
        $_SESSION['user_id'] = $user_id;
 
        $this->ajaxReturn($GtSdk->get_response_str());
 
    }
 
    /**
     * 极验二次验证
     * @return bool
     */
    public  function VerifyLoginServlet(){
        vendor('Geetestlib.class#geetestlib');
        session_start();
        $GtSdk = new GeetestLib(C("CAPTCHA_ID"), C("PRIVATE_KEY"));
        $user_id = $_SESSION['user_id'];
        if ($_SESSION['gtserver'] == 1) {
            $result = $GtSdk->success_validate($_POST['geetest_challenge'], 
            $_POST['geetest_validate'], 
            $_POST['geetest_seccode'], $user_id);
            if ($result) {
                return true;
            } else{
                return false;
            }
        }else{
            if ($GtSdk->fail_validate($_POST['geetest_challenge'],
            $_POST['geetest_validate'],
            $_POST['geetest_seccode'])) {
                return true;
            }else{
                return false;
            }
        }
    }

7、登录验证

/**
     * 用户登录提交
     * @param $loginName
     * @param $loginPassword
     * @param bool $isRemember
     */
    public function  btnLogin($loginName,$loginPassword,$isRemember = true){
 
        if($this->VerifyLoginServlet()){
            $Model = new Model();
            $sqlNews = "";
            $DataUser = $Model->query( $sqlNews );
            if($DataUser){
            $Model->execute("sql 语句");
 
            if($DataUser[0]['PassWord'] == $loginPassword){
                if($isRemember == ture){
                     session('User',$DataUser[0]);
                }else{
                    session(null);
                }
                $this->ajaxReturn(0,"登录成功!",1,'JSON');
            }else{
                $this->ajaxReturn(0,"密码不正确!",0,'JSON');
            }
        }else{
            $this->ajaxReturn(0,"用户名不存在!",0,'JSON');
        }
        }else{
            $this->ajaxReturn(0,"验证不正确!",0,'JSON');
        }
 
    }
0

评论 (0)

取消