PHP code example of aivanouski / yii2-invisible-recaptcha

1. Go to this page and download the library: Download aivanouski/yii2-invisible-recaptcha 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/ */

    

aivanouski / yii2-invisible-recaptcha example snippets



use szaboolcs\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();


namespace app\controllers;

use szaboolcs\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(InvisibleRecaptchaValidatior::POST_ELEMENT)) && Yii::$app->user->login($model->getUser())) {
      return $this->goHome();
    }

    return $this->render('login', [
      'model' => $model
    ]);
  }
}


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;
  }
}