Home > Backend Development > PHP Tutorial > PHP extreme verification code example explanation

PHP extreme verification code example explanation

高洛峰
Release: 2023-03-05 11:24:02
Original
1987 people have browsed it

You can find this article, which means you are no longer completely unfamiliar with Jiexian verification. I won’t go into all the nonsense and just start explaining how to use it. But before that, paste a few The URL you may use:

Official website: http://www.geetest.com/

Official document: http://www.geetest.com/install/sections/idx -basic-introduction.html

github:https://github.com/GeeTeam/gt-php-sdk.git

How to use:

First To confirm the front-end usage page, such as the login page


 

1. Access the imported class library on the login page:

If your website uses https, you only need to change the place where Jiqianku is introduced to the https protocol, and there is no need to change other places. For example, replace it with the following code:


 

2. Initialize the front end

The following code It needs to be executed after the page is loaded. If you use jQuery, you can write it in $(function(){});

$.ajax({
  // 获取id,challenge,success(是否启用failback)
  url: "../web/StartCaptchaServlet.php?t=" + (new Date()).getTime(), // 加随机数防止缓存
  type: "get",
  dataType: "json",
  success: function (data) {
    // 使用initGeetest接口
    // 参数1:配置参数
    // 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件
    initGeetest({
      gt: data.gt,
      challenge: data.challenge,
      product: "popup", // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效
      offline: !data.success // 表示用户后台检测极验服务器是否宕机,与SDK配合,用户一般不需要关注
    }, handlerPopup);
  }
});
Copy after login

The above code means that after the page is loaded, you need to go to the URL you specified. Obtain the verification code information on the address. As for what is written in the above URL address "../web/StartCaptchaServlet.php", we will explain this in detail in the server-side code deployment.

But it should be noted that there is a callback function called "handlerPopup" in the above code. This function is the real initialization code you need to verify the code: as follows:

// 代码详细说明
var handlerPopup = function (captchaObj) {
  // 注册提交按钮事件,比如在登陆页面的登陆按钮
  $("#popup-submit").click(function () {
    // 此处省略在登陆界面中,获取登陆数据的一些步骤
     
    // 先校验是否点击了验证码
    var validate = captchaObj.getValidate();
    if (!validate) {
      alert('请先完成验证!');
      return;
    }
    // 提交验证码信息,比如登陆页面,你需要提交登陆信息,用户名和密码等登陆数据
    $.ajax({
      url: "../web/VerifyLoginServlet.php",
      type: "post",
      // dataType: "json",
      data: {
        // 用户名和密码等其他数据,自己获取,不做演示
        username:username,
        password:password,
        // 验证码数据,这些数据不用自己获取
        // 这是二次验证所需的三个值
        // 当然,你也可以直接设置验证码单独校验,省略其他信息
        geetest_challenge: validate.geetest_challenge,
        geetest_validate: validate.geetest_validate,
        geetest_seccode: validate.geetest_seccode
      },
      // 这里是正确返回处理结果的处理函数
      // 假设你就返回了1,2,3
      // 当然,正常情况是返回JSON数据
      success: function (result) {
        // 1表示验证码验证失败
        if (result == "1") {
          alert("验证码验证失败!");
        }else if (result == "2") {
          alert("用户名或密码错误!");
        }else if (result == "3") {
          alert("登陆成功!");
          // 登陆成功了,可以在这里做其他处理
        }else{
          alert("登陆错误!");
        }
      }
    });
  });
  // 弹出式需要绑定触发验证码弹出按钮
  // 比如在登陆页面,这个触发按钮就是登陆按钮
  captchaObj.bindOn("#popup-submit");
    
  // 将验证码加到id为captcha的元素里
  // 验证码将会在下面指定的元素中显示出来
  captchaObj.appendTo("#popup-captcha");
    
  // 更多接口参考:http://www.geetest.com/install/sections/idx-client-sdk.html
};
  
Copy after login

At this point, the front-end All settings have been written, official documentation: http://www.geetest.com/install/sections/idx-client-sdk.html

3. Server-side deployment (PHP)

In the first step, we set up such an address "../web/StartCaptchaServlet.php", what to write in this address:

<?php
// 引入文件
require_once dirname(dirname(__FILE__)) . &#39;/lib/class.geetestlib.php&#39;;
require_once dirname(dirname(__FILE__)) . &#39;/config/config.php&#39;;
// 实例化,实例化的参数在config中配置,分别是:验证ID 和 验证Key,如何获取下面会说。
$GtSdk = new GeetestLib(CAPTCHA_ID, PRIVATE_KEY);
session_start();
// 这个是用户的标识,或者说是给极验服务器区分的标识,如果你项目没有预先设置,可以像下面这样设置:
if(!isset($_SESSION[&#39;user_id&#39;])){
  $_SESSION[&#39;user_id&#39;]=uniqid();// 生成一个唯一ID
}
$user_id = $_SESSION[&#39;user_id&#39;];
// 或者,你就直接写成:
// $user_id = "test";
  
// 生成验证码信息,并返回给客户端
$status = $GtSdk->pre_process($user_id);
$_SESSION[&#39;gtserver&#39;] = $status;
$_SESSION[&#39;user_id&#39;] = $user_id;
echo $GtSdk->get_response_str();
?>
  
Copy after login

How to get the verification ID and verification Key:

1. Register an account from the verification background
2. Add verification, you can get a set of currently verified ID/Key
3. Replace the ID and Key to captcha_id/ in the config.php file. The value of the private_key variable

4. Server-side verification (secondary verification) after clicking the submit button

For example, as mentioned above, when the user logs in, you put the user name, password and verification code information All have been submitted to the server. At this time, you can do verification:

<?php
// 引入文件
require_once dirname(dirname(__FILE__)) . &#39;/lib/class.geetestlib.php&#39;;
require_once dirname(dirname(__FILE__)) . &#39;/config/config.php&#39;;
session_start();
$GtSdk = new GeetestLib(CAPTCHA_ID, PRIVATE_KEY);
  
// 比如你设置了一个验证码是否验证通过的标识
$code_flag=false;
  
// 这里获取你之前设置的user_id,传送给极验服务器做校验
$user_id = $_SESSION[&#39;user_id&#39;];
if ($_SESSION[&#39;gtserver&#39;] == 1) {
  $result = $GtSdk->success_validate($_POST[&#39;geetest_challenge&#39;], $_POST[&#39;geetest_validate&#39;], $_POST[&#39;geetest_seccode&#39;], $user_id);
  if ($result) {
    // 验证码验证成功
    $code_flag=true;
  }
}else{
  if ($GtSdk->fail_validate($_POST[&#39;geetest_challenge&#39;],$_POST[&#39;geetest_validate&#39;],$_POST[&#39;geetest_seccode&#39;])) {
     // 验证码验证成功
    $code_flag=true;
  }
}
  
// 如果验证码验证成功,再进行其他校验
if($code_flag){
  // 其他验证操作
  // 用户名不对时,返回2,其他错误返回3,等等。。。。
}else{
  // 验证码验证失败,返回1,这里和上面相呼应,当然我的项目没有简单的返回1,而是返回了JSON数据
  echo 1;
  exit(-1);
}
?>
  
Copy after login

Thank you for reading, I hope it can help everyone, thank you everyone for your support of this site!

For more relevant articles on PHP’s extreme verification code examples, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template