andrewdanilov/yii2-grecaptchav3

用于使用Google reCAPTCHA v3的类和资源

安装: 239

依赖: 0

建议: 0

安全: 0

星星: 1

关注者: 2

分支: 0

开放问题: 0

类型:yii2-extension

1.0.2 2021-08-29 11:13 UTC

This package is auto-updated.

Last update: 2024-09-29 04:43:33 UTC


README

用于使用Google reCAPTCHA v3的类和资源

安装

安装此扩展的首选方式是通过 Composer

运行以下命令之一:

composer require andrewdanilov/yii2-grecaptchav3 "~1.0.0"

或添加以下内容到您的 composer.json 文件的 require 部分。

"andrewdanilov/yii2-grecaptchav3": "~1.0.0"

使用方法

在网站配置中

<?php
// ...
return [
	// ...
	'components' => [
		// ...
		'recaptcha' => [
			'class' => \andrewdanilov\grecaptchav3\Recaptcha::class,
			'sitekey' => 'place your sitekey here', // optional, if not set, you need to define it in widget config
			'secret' => 'place yout secret here',
		],
	],
];

在控制器动作中

<?php
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
	$token = Yii::$app->request->post('g-recaptcha-token');
	$action = Yii::$app->request->post('g-recaptcha-action');
	if (Yii::$app->recaptcha->verify($token, $action)) {
		// ...
		// additionally you can check your action here
	} else {
		$model->addError('recaptcha', 'Recaptcha error');
	}
}

在视图中

<form action="/register" method="post" id="registerForm">
	<!-- form fields -->
</form>

<?= \andrewdanilov\grecaptchav3\RecaptchaInit::widget([
	'formID' => 'registerForm',
	'action' => 'my_register_action', // optional, default is <controller_id>_<action_id>_<widget_id>
	'sitekey' => 'place your sitekey here', // optional, if you defined sitekey in component config
]) ?>

小部件参数 action 必须只包含 [A-Za-z_] 集合中的字符,并且必须在页面上每个表单中是唯一的。