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
The rough logic of logging in
Register a Geetest account
Register a behavioral verification in the background management of "JiXian"
Configure our controller according to the official Demo and routing
Configure our login template according to the official Demo
Test
##Geetest integration detailed process
1. Implement the general logic of loginCreate the controller php artisan make:controller GeetestController<?php namespace App\Http\Controllers; use Illuminate\Http\Request; /** * 这是一个集成 Geetest 验证码的 Demo 类 */ class GeetestController extends Controller { /** * 导入登录视图 */ public function login() { return view('Geetest/login'); } /** * 验证用户信息 */ public function check() { return '用户已经在前端通过了验证码验证, 你可以在这里完善后续的逻辑'; } }
<?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('Geetest/login'); } /** * 验证用户信息 */ public function check() { return '用户已经在前端通过了验证码验证, 你可以在这里完善后续的逻辑'; } /** * 实现验证功能: 直接复制官方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['gtserver'] = $status; $_SESSION['user_id'] = $data['user_id']; echo $GtSdk->get_response_str(); } }
// 集成 Geetest 验证码 Route::get('GeetestLogin', 'GeetestController@login'); //登录页面 Route::get('GeetestCheck', 'GeetestController@check'); //登录验证 (我们没写具体逻辑) Route::get('GeetestStartCaptchaServlet', 'GeetestController@startCaptchaServlet'); // 调用方法启用验证码
resources/views/Geetest/login.blade.php
Need to import jquery (we use npm run dev compiled app.js to integrate jquery)<!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 optimizedIt is best not to use a "controller" as the core class library. You should find a way to integrate GeetestLib into another placeLaravel 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!