anam / captcha
reCAPTCHA 和不可见 reCAPTCHA for Laravel。reCAPTCHA 可保护您的应用程序免受垃圾邮件和机器人的侵害。
Requires
- php: >=7.0.0
- google/recaptcha: ^1.1
Requires (Dev)
- mockery/mockery: ^1.0
- orchestra/testbench: ^3.5
- phpunit/phpunit: ^6.4
This package is not auto-updated.
Last update: 2024-09-15 04:31:24 UTC
README
reCAPTCHA 可保护您的应用程序免受垃圾邮件和机器人的侵害。本软件包与 Laravel 5.5 进行了测试。
要求
- PHP 7.0+
安装
Captcha
通过 Composer 提供
$ composer require anam/captcha
或者,直接在您的 composer.json 文件中添加依赖项
"require": { "anam/captcha": "~1.0" }
集成
Laravel 5.5+ 集成
软件包发现
Anam\Captcha
利用 Laravel 的包自动发现功能。因此,您无需手动在 Laravel 应用程序的 config/app.php 文件中添加 Service provider 和 Facade。Laravel 将自动为您注册服务提供者和外观。
Laravel < 5.5 集成
Captcha 提供了 Service provider 和 Facade 以便于集成。
在您安装了 anam/captcha
之后,打开 Laravel 所包含的 config/app.php
文件,并添加以下行。
在 $providers
数组中添加以下服务提供者。
'Anam\Captcha\ServiceProvider\CaptchaServiceProvider'
将本软件包的外观添加到 $aliases
数组。
'Captcha' => 'Anam\Captcha\Facade\Captcha'
您现在可以在以下示例中使用此外观,而不是自己实例化转换器。
配置
首先,在 https://www.google.com/recaptcha/admin 为您的网站注册密钥
在 .env
文件中添加 RECAPTCHA_SITE_KEY
和 RECAPTCHA_SECRET
RECAPTCHA_SITE_KEY=site_key
RECAPTCHA_SECRET=secret
运行 vendor publish 将 captcha.php 文件添加到配置中
php artisan vendor:publish --tag=CaptchaConfig
默认情况下,该软件包将尝试从环境中加载密钥。但是,您可以手动设置它们
$captcha = new \Anam\Captcha\Captcha('recaptcha_secret');
Blade 指令
// reCAPTCHA v2 @captcha(site_key) // Invisible reCAPTCHA @invisiblecaptcha(site_key)
使用方法
客户端
reCAPTCHA V2
只需将 @captcha()
blade 指令添加到表单中。
<form method="POST" action="/captcha" id="captcha-form"> {{ csrf_field() }} <label>Name</label> <input type="text" name="name"> <label>Your message</label> <textarea name="message" rows="5"></textarea> <br> @captcha() <br> <input type="submit" value="Submit"> </form>
有关更高级的集成,请访问以下链接:https://developers.google.com/recaptcha/docs/display
不可见 reCAPTCHA
将 @invisiblecaptcha()
指令添加到您希望出现提交按钮的表单中。请注意,@invisiblecaptcha
指令将为您注入提交按钮。如果您想自定义提交按钮样式,可以使用可用的 .g-recaptcha
类。
<form method="POST" action="/captcha" id="captcha-form"> {{ csrf_field() }} <label>Name</label> <input type="text" name="name"> <label>Your message</label> <textarea name="message" rows="5"></textarea> <br> @invisiblecaptcha() </form>
注意事项:如果视图包含多个表单,则 @invisiblecaptcha()
可能不会按预期工作,因为它将提交第一个表单。在这些情况下,您必须手动集成 reCAPTCHA。
请访问以下链接:https://developers.google.com/recaptcha/docs/invisible
服务器端
处理请求
use Anam\Captcha\Captcha; use Illuminate\Http\Request; class CaptchaController extends Controller { /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @param \Anam\Captcha\Captcha $captcha * @return \Illuminate\Http\Response */ public function store(Request $request, Captcha $captcha) { $response = $captcha->check($request); if (! $response->isVerified()) { dd($response->errors()); } dd($response->hostname()); } }
示例
Laravel 用户注册控制器
app\Http\Controllers\Auth\RegisterController.php
<?php namespace App\Http\Controllers\Auth; use App\User; use App\Http\Controllers\Controller; use App\Rules\GoogleRecaptcha; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; use Illuminate\Foundation\Auth\RegistersUsers; class RegisterController extends Controller { /* |-------------------------------------------------------------------------- | Register Controller |-------------------------------------------------------------------------- | | This controller handles the registration of new users as well as their | validation and creation. By default this controller uses a trait to | provide this functionality without requiring any additional code. | */ use RegistersUsers; /** * Where to redirect users after registration. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest'); } /** * Get a validator for an incoming registration request. * * @param array $data * @return \Illuminate\Contracts\Validation\Validator */ protected function validator(array $data) { $messages = [ 'g-recaptcha-response.required' => 'You must verify that you are not a robot.', ]; return Validator::make($data, [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => ['required', 'string', 'min:8', 'confirmed'], 'g-recaptcha-response' => ['required', new GoogleRecaptcha] ], $messages); } /** * Create a new user instance after a valid registration. * * @param array $data * @return \App\User */ protected function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), ]); } }
app\Rules\GoogleRecaptcha.php
<?php namespace App\Rules; use Anam\Captcha\Captcha; use Illuminate\Contracts\Validation\Rule; class GoogleRecaptcha implements Rule { /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value) { $captcha = new Captcha(); $response = $captcha->check(request()); return $response->isVerified(); } /** * Get the validation error message. * * @return string */ public function message() { return 'Are you a robot?'; } }
致谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 LICENSE