Home  >  Article  >  Backend Development  >  How to integrate Geetest verification code with Laravel

How to integrate Geetest verification code with Laravel

不言
不言Original
2018-06-01 10:00:471743browse

This article mainly introduces the method of integrating Geetestverification code with Laravel. It has a certain reference value. Now I share it with you. Friends in need can refer to it

Geetest integration process

  1. The rough logic of logging in

  2. Register a Geetest account

  3. Register a behavioral verification in the background management of "JiXian"

  4. Configure our controller according to the official Demo and routing

  5. Configure our login template according to the official Demo

  6. Test

##Geetest integration detailed process

1. Implement the general logic of login

Create the controller php artisan make:controller GeetestController


The edit controller/app/Http/Controllers/GeetestController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

/**
* 这是一个集成 Geetest 验证码的 Demo 类
*/
class GeetestController extends Controller
{ 
 /**
 * 导入登录视图
 */
 public function login() {
  return view(&#39;Geetest/login&#39;);
 }

 /**
 * 验证用户信息
 */
 public function check() {
  return &#39;用户已经在前端通过了验证码验证, 你可以在这里完善后续的逻辑&#39;;
 }
}

view is a simple form, omitted.


2. Omit => "Register"

3. Omit => "Backend login" => "Behavior verification" => Apply for an id & key

4. Configure the controller and routing

First of all, the core class library provided by the Demo is a class file called class.geetestlib.php, and the class name is GeetestLib. We create a controller with the same class name to replace it php artisan make:controller GeetestLib


Don’t copy the class, just copy the content in the class


GeetestController control Server implementation logic

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\GeetestLib; // 我们创建然后拷贝得来的 GeetestLib 核心库

/**
* 这是一个集成 Geetest 验证码的 Demo 类
*/
class GeetestController extends Controller
{ 
 // 这里配置 id & key
 private $captchaId = "5d467a3cb22a9310837d51720c5251f0";
 private $privateKey = "40764e6b94344f780d4b6b07148c9495";

 /**
 * 导入登录视图
 */
 public function login() {
  return view(&#39;Geetest/login&#39;);
 }

 /**
 * 验证用户信息
 */
 public function check() {
  return &#39;用户已经在前端通过了验证码验证, 你可以在这里完善后续的逻辑&#39;;
 }

 /**
 * 实现验证功能: 直接复制官方demo提供得
 */
 public function startCaptchaServlet() {
  // 这里使用配置的 id & key
  $GtSdk = new GeetestLib($this->captchaId, $this->privateKey);
  session_start();
  
  $data = array(
   "user_id" => "test", # 网站用户id
   "client_type" => "web", #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式
   "ip_address" => "127.0.0.1" # 请在此处传输用户请求验证时所携带的IP
  );
  
  $status = $GtSdk->pre_process($data, 1);
  $_SESSION[&#39;gtserver&#39;] = $status;
  $_SESSION[&#39;user_id&#39;] = $data[&#39;user_id&#39;];
  echo $GtSdk->get_response_str();
 }
}

Configure routing/routes/web.php

// 集成 Geetest 验证码
Route::get(&#39;GeetestLogin&#39;, &#39;GeetestController@login&#39;); //登录页面
Route::get(&#39;GeetestCheck&#39;, &#39;GeetestController@check&#39;); //登录验证 (我们没写具体逻辑)
Route::get(&#39;GeetestStartCaptchaServlet&#39;, &#39;GeetestController@startCaptchaServlet&#39;); // 调用方法启用验证码

5. Improve the login template/

resources/views/Geetest/login.blade.php

Need to import jquery (we use npm run dev compiled app.js to integrate jquery)


Need to import Demo and give gt.js, we put it under public/js

198b1cd9e2d3dcaea0fb49ed1e069ee72cacc6d41bbb37262a98f745aa00fbf0

In fact, in theory, it can also be placed under /resouces/assets/js/, and In /resouces/assets/js/app.js, require comes in so that it can participate in compilation, and it can be packaged and integrated directly in public/js to take effect.


On the template, two style classes need to be defined.show & .hide => The styles used for gt.js control prompt information can also be written under /resouces/assets/sass/


Submit an id to the "Login" button in the form

Copy the front-end logic js provided in the Demo, pay attention to binding this button

Pay attention to the .ajax configuration The url must be the path we defined in web.php with 'GeetestStartCaptchaServlet'

Specific code

<!DOCTYPE html>
<html lang="zh-CN">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">

 <!-- 这是我们用 npm run dev 编译后的 css / js -->
 <link rel="stylesheet" href="/css/app.css" rel="external nofollow" >
 <script src="/js/app.js"></script>

 <!-- 这里需要用到两个样式 -->
 <style>
  .show {
   display: block;
  }
  .hide {
   display: none;
  }
 </style>

 <title> Geetest 集成 Demo</title>
</head>
<body>
 <p class="container">
  <p class="row">
   <p class="col-lg-12">
    <h1 class="text-center">Geetest 集成 Demo
     <small>
      <a href="http://www.geetest.com/" rel="external nofollow" rel="external nofollow" > Geetest 官方网站 </a>
     </small> 
    </h1>
   </p>
   <p class="col-lg-12">
    <form method="GET" action="/GeetestCheck">
     <p class="form-group">
      <label for="exampleInputEmail1">模拟邮箱地址</label>
      <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="请输入邮箱...">
      <small id="emailHelp" class="form-text text-muted">我们不会公开您的邮箱</small>
     </p>
     <p class="form-group">
      <label for="exampleInputPassword1">模拟密码</label>
      <input type="password" class="form-control" id="exampleInputPassword1" placeholder="请输入密码...">
     </p>
     <p class="form-group">
      <p id="embed-captcha"></p>
      <p id="wait" class="show">正在加载验证码......</p>
      <p id="notice" class="hide">请先完成验证</p>
     </p>
     <!-- 这里需要绑定一个按钮 -->
     <button type="submit" class="btn btn-primary" id="embed-submit">登录</button>
    </form>
   </p>
  </p>
 </p>

 <!-- 引用 gt.js -->
 <script src="/js/gt.js"></script>
 <!-- 直接复制官方Demo里的js代码 -->
 <script>
  var handlerEmbed = function (captchaObj) {
   $("#embed-submit").click(function (e) {
    var validate = captchaObj.getValidate();
    if (!validate) {
     $("#notice")[0].className = "show";
     setTimeout(function () {
      $("#notice")[0].className = "hide";
     }, 2000);
     e.preventDefault();
    }
   });
   // 将验证码加到id为captcha的元素里,同时会有三个input的值:geetest_challenge, geetest_validate, geetest_seccode
   captchaObj.appendTo("#embed-captcha");
   captchaObj.onReady(function () {
    $("#wait")[0].className = "hide";
   });
   // 更多接口参考:http://www.geetest.com/install/sections/idx-client-sdk.html
  };
  $.ajax({
   // 获取id,challenge,success(是否启用failback)
   url: "/GeetestStartCaptchaServlet", // 加随机数防止缓存
   type: "get",
   dataType: "json",
   success: function (data) {
    console.log(data);
    // 使用initGeetest接口
    // 参数1:配置参数
    // 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件
    initGeetest({
     gt: data.gt,
     challenge: data.challenge,
     new_captcha: data.new_captcha,
     product: "embed", // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效
     offline: !data.success // 表示用户后台检测极验服务器是否宕机,一般不需要关注
     // 更多配置参数请参见:http://www.geetest.com/install/sections/idx-client-sdk.html#config
    }, handlerEmbed);
   }
  });
 </script>
</body>
</html>

Test successful

Area that can be optimized

It is best not to use a "controller" as the core class library. You should find a way to integrate GeetestLib into another place


js on the view template & css should be written in resources/assets to participate in the compilation of generating app.css & app.js


We have not written the specific login logic. You should also be able to confirm whether the Geetest verification is successful in the login verification check() method. You can refer to Demo


Related recommendations:


Laravel framework implements the model layer Example of CURD operation

The above is the detailed content of How to integrate Geetest verification code with Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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