Google Invisible reCaptcha for Yii2
Based on reCaptcha API 2.0. Fork of szaboolcs/yii2-invisible-recaptcha., (*1)
Installation
The preferred way to install this extension is through composer., (*2)
php composer.phar require --prefer-dist "pctux/yii2-invisible-recaptcha" "*"
, (*3)
or add, (*4)
"pctux/yii2-invisible-recaptcha" : "*"
, (*5)
to the require section of your application's composer.json file., (*6)
- Sign up for an reCAPTCHA API keys.
- Add the next component to your configuration file (web.php)
'components' => [
'captcha' => [
'name' => 'captcha',
'class' => 'pctux\recaptcha\InvisibleRecaptcha',
'siteKey' => 'your siteKey',
'secret' => 'your secret'
]
]
Usage
login.php, (*7)
<?php
use pctux\recaptcha\InvisibleRecaptcha;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin([
'id' => 'login-form'
]);
echo $form->field($model, 'username');
echo $form->field($model, 'password');
echo InvisibleRecaptcha::widget([
'name' => 'Submit',
'formSelector' => '#login-form'
]);
ActiveForm::end();
LoginController.php, (*8)
<?php
namespace app\controllers;
use pctux\recaptcha\InvisibleRecaptchaValidator;
use app\models\Login;
use Yii;
class LoginController
{
public function actionLoginForm()
{
$model = new Login();
return $this->render('login', [
'model' => $model
]);
}
public function actionLogin()
{
$model = new Login();
$model->load(Yii::$app->request->post());
if ($model->validate() && InvisibleRecaptchaValidator::validate(Yii::$app->request->post(InvisibleRecaptchaValidator::POST_ELEMENT)) && Yii::$app->user->login($model->getUser())) {
return $this->goHome();
}
return $this->render('login', [
'model' => $model
]);
}
}
Login.php, (*9)
<?php
use yii\base\Model;
use app\models\User;
class Login extends Model
{
public $username;
public $password;
private $_user;
public function rules()
{
// ...
}
public function getUser()
{
if (!$this->_user) {
$this->_user = User::find()->byUsername($this->username)->one();
}
return $this->_user;
}
}