Home  >  Article  >  PHP Framework  >  Detailed explanation of ThinkPHP5's implementation of the geetest sliding verification code function

Detailed explanation of ThinkPHP5's implementation of the geetest sliding verification code function

藏色散人
藏色散人forward
2021-04-01 17:22:363447browse

The following tutorial column will give you a detailed explanation of how ThinkPHP5 implements the geetest sliding verification code function. I hope it will be helpful to friends in need! ThinkPHP5 implements the geetest sliding verification code function

Now many websites, such as Taobao, JD.com, etc., have switched to using the geetest sliding verification code to log in. This This method has a better experience than the traditional verification code method, reduces user input errors, and can also prevent theft and swiping. Nowadays, many of the best tests are provided by third parties, and many of them are paid. Here we mainly introduce the geetest, the ultimate sliding verification code of the thinkphp integration series, official website: http://www.geetest.com

The details are as follows:

One: Register and get key

Register; create application; get key;

Two: Import sdk

/ThinkPHP/Library/Org/ Xb/GeetestLip.class.php (Here GeetestLip.class.php is the geetest class file I renamed. The original name was class.geetestlib.php) Detailed explanation of ThinkPHP5s implementation of the geetest sliding verification code function

This involves the introduction of third-party classes by thinkphp. I put the third-party classes under Org/Util/Xb and added the namespace to the class files as follows, otherwise the files cannot be found when instantiating the classDetailed explanation of ThinkPHP5s implementation of the geetest sliding verification code function

Three: Generate verification styleDetailed explanation of ThinkPHP5s implementation of the geetest sliding verification code functionadmin/view/public/cdtsh_log_smfyws.php

nbsp;html>


  <meta>
  <title>网站管理系统后台</title>
  <script></script>
  <link>
  <script></script>
  <script></script>
  <link>
  <script>
    $(document).ready(function(){
      var verifyimg = $(".verifyimg").attr("src");
      $(".reloadverify").click(function(){
        if( verifyimg.indexOf(&#39;?&#39;)>0){
          $(".verifyimg").attr("src", verifyimg+&#39;&random=&#39;+Math.random());
        }else{
          $(".verifyimg").attr("src", verifyimg.replace(/\?.*$/,&#39;&#39;)+&#39;?&#39;+Math.random());
        }
      });
    });
  </script>


<p>
  </p><h1><a>后台管理中心</a></h1>
  
    

      

            
  •                               
  •         
  •                               
  •         
  •                      

      Detailed explanation of ThinkPHP5's implementation of the geetest sliding verification code function 

                       
  •       
      
                                    

          
              
<script></script> <script> var handler = function (captchaObj) { // 将验证码加到id为captcha的元素里 captchaObj.appendTo("#captcha"); }; // 获取验证码 $.get("{:U(&#39;Admin/Public/verifys&#39;)}", function(data) { // 使用initGeetest接口 // 参数1:配置参数,与创建Geetest实例时接受的参数一致 // 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件 initGeetest({ gt: data.gt, challenge: data.challenge, product: "float", // 产品形式 offline: !data.success, new_captcha:&#39;true&#39;, width:&#39;260px&#39;, }, handler); },&#39;json&#39;); </script>

Four: Verification function

/Application/Common/Common/function.php

/**
 * geetest检测验证码
 */
function geetest_chcek_verify($data){
  $geetest_id = "7149e2021d7938157e";
  $geetest_key = "62b92039e1e9cf9455";
  $geetest=new \Org\Util\Xb\GeetestLib($geetest_id,$geetest_key);
  $user_id=$_SESSION['geetest']['user_id'];
  $ip_address=$_SESSION['geetest']['ip_address'];
  $dataa = array(
    "user_id" => $user_id, # 网站用户id
    "client_type" => "web", #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式
    "ip_address" => $ip_address, # 请在此处传输用户请求验证时所携带的IP
  );
  if ($_SESSION['geetest']['gtserver']==1){
    $result=$geetest->success_validate($data['geetest_challenge'], $data['geetest_validate'], $data['geetest_seccode'], $dataa);
    //return $result;
    if ($result) {
      //return 11;
      return true;
    } else{
      //return 22;
      return false;
    }
  }else{
    if ($geetest->fail_validate($data['geetest_challenge'],$data['geetest_validate'],$data['geetest_seccode'])) {
      //return 33;
      return true;  
    }else{
      //return 44;
      return false;
    }
  }
}
//获取id地址
function GetIP() {
  if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
    $cip = $_SERVER["HTTP_CLIENT_IP"];
  } elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
    $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
  } elseif (!empty($_SERVER["REMOTE_ADDR"])) {
    $cip = $_SERVER["REMOTE_ADDR"];
  } else {
    $cip = "无法获取!";
  }
  return $cip;
}

5: php generates verification code and verifies

//极速验证
  public function verifys(){
    //require_once dirname(dirname(dirname(__FILE__))) . '/lib/class.geetestlib.php';
    //require_once dirname(dirname(__FILE__)) . '/config/config.php';
    // $GtSdk = new GeetestLib(CAPTCHA_ID, PRIVATE_KEY);
    $geetest_id = "7149e2021d7938157e9";
    $geetest_key = "62b92039e1e9cf";
    $geetest=new \Org\Util\Xb\GeetestLib($geetest_id,$geetest_key);
    //dump($geetest);die;
    $user_id = "test";
    $data = array(
      "user_id" => $user_id, # 网站用户id
      "client_type" => "web", #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式
      "ip_address" => GetIP(), # 请在此处传输用户请求验证时所携带的IP
    );
    $status = $geetest->pre_process($data,1);
    //dump($status);
    $_SESSION['geetest']=array(
      'gtserver'=>$status,
      'user_id'=>$user_id,
      'ip_address'=>GetIP(),
      );
    echo $geetest->get_response_str();
  }
 public function cdtsh_log_smfyws() {
    if ($_SESSION['userid']) {
      $this->redirect('Admin/Index/Index');
    } else {
      if (IS_POST) {
        $username = $_POST['username'];
        $password = $_POST['password'];
        //$geetest_challenge = $_POST['geetest_challenge'];
        //$geetest_validate = $_POST['geetest_validate'];
        //$geetest_seccode = $_POST['geetest_seccode'];
        $data=I('post.');
        if($data['geetest_challenge']=="" || $data['geetest_validate']=="" ||$data['geetest_seccode']=="" ){
          $this->error('请进行图形验证');
        }else{
          //dump(geetest_chcek_verify($data));
          if (geetest_chcek_verify($data)){
            //echo '验证成功';
            if ($this->loginAdmin($username, $password)) {
              $data = M("User")->where("username='".$username."' and password='".md5($password)."'")->find();
              if ($data["status"] != 1) {
                //判断是否禁用
                $this->recordLoginAdmin($_POST['username'], $_POST['password'], 0, "账号禁用"); //记录登录日志
                $this->error('该帐号禁用');
              } else {
                $save["lastlogin_time"] = time();
                $save["lastlogin_ip"] = get_client_ip();
                $save["login_num"] = $data["login_num"] + 1;
                $status = M("user")->where(array("id" => $data['id']))->save($save);
                $_SESSION['userid'] = $data['id'];
                $_SESSION['user'] = $data['username'];
                $_SESSION['rid'] = $data['a_Id'];
                $this->recordLoginAdmin($_POST['username'], $_POST['password'], 1); //记录登录日志
                $this->redirect('Admin/Index/Index');
                //$this->success('登录成功',U('Admin/Index/Index'));
              }
            } else {
              $this->recordLoginAdmin($_POST['username'], $_POST['password'], 0, "账号密码错误"); //记录登录日志
              $this->error('登录失败');
            }
          }else{
            //echo '图形验证失败';
            $this->error('图形验证失败');
          }
        }
      } else {
        $this->display();
      }
    }
  }

The rendering is as follows:

Login page

##After clicking verificationDetailed explanation of ThinkPHP5s implementation of the geetest sliding verification code function

The above is the detailed content of Detailed explanation of ThinkPHP5's implementation of the geetest sliding verification code function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete