This article mainly introduces the method of integrating Geetest verification code with Laravel. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
Geetest integration process
The general logic of realizing login
Register a JiExperience account
Register a behavioral verification in the background management of “JiExperience”
Configure our controller and routing according to the official Demo
Configure our login template according to the official Demo
Test
Geetest integration detailed process
1. Implement the general logic of login
Create the controller php artisan make:controller GeetestController
Edit Controller/app/Http/Controllers/GeetestController
Copy after login
The 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 Device implementation logic
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(); } }
Configuration routing/routes/web.php
// 集成 Geetest 验证码 Route::get('GeetestLogin', 'GeetestController@login'); //登录页面 Route::get('GeetestCheck', 'GeetestController@check'); //登录验证 (我们没写具体逻辑) Route::get('GeetestStartCaptchaServlet', 'GeetestController@startCaptchaServlet'); // 调用方法启用验证码
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 to give gt.js, we put it under public/js
In fact, in theory, it can also be placed under /resouces/assets/js/, and require in /resouces/assets/js/app.js to let it participate in being compiled, and package it directly in public/js The integration takes 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
Geetest 集成 Demo
Geetest 集成 Demo Geetest 官方网站
Test Success
Things that can be optimized
It is best not to use a "controller" as the core class library. GeetestLib should be integrated into another place
The js & css on the view template 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
. The above is the entire content of this article. I hope it will be helpful to everyone's study. I also hope that everyone will learn more. Support PHP Chinese website.
Phpstorm Xdebug breakpoint debugging method for PHP php instance
php strftime function gets the date and time php basics
Sample code for PHP multi-dimensional array to specify multi-field sorting_php example
The above is the detailed content of Laravel integrates Geetest verification code method php example. For more information, please follow other related articles on the PHP Chinese website!