PHP code example of andrewdanilov / yii2-grecaptchav3

1. Go to this page and download the library: Download andrewdanilov/yii2-grecaptchav3 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andrewdanilov / yii2-grecaptchav3 example snippets



// ...
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',
		],
	],
];


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
])