• Like Us On


How to install google recaptcha in yii2 advanced

How to install google recaptcha in yii2 advanced

Google i am not robot reCaptcha is very famous these days.It is an ease of use for site visitors yet it provide ultimate security from spammers.Here is step by step guide to install it on Php framework Yii2.there are lot of extensions by which we can install Google reCaptcha on yii2.But my favorite is himiklab/yii2-recaptcha-widget .So lets start the process.

1.Go to the official Google reCAPTCHA site and click on Get reCAPTCHA.

2.This will take you to admin page as shown in below image.Now enter a label for your captcha.This is for reference only as you can have multiple captcha on multiple websites.Select the Recaptcha v2 and then enter domains.You only need to enter domain name.All subdomains will be added automatically.For testing on localhost you can also add localhost and .Now click save changes on bottom.

3.Now you will be taken to new window containing your key and secret.You should copy and paste these for later use.

4.Now install himiklab/yii2-recaptcha-widget through composer.

5.Now open your model and add validation rules to it.

class Subscribe extends \yii\db\ActiveRecord
    public $reCaptcha;

    public static function tableName()
        return '{{%subscribe}}';
    public function rules()
        return [
            [['email'], 'required'],
            [['reCaptcha'], \himiklab\yii2\recaptcha\ReCaptchaValidator::className()]


6. And in frontend/config.main.php under components add below code.

'components' => [
    'reCaptcha' => [
        'name' => 'reCaptcha',
        'class' => 'himiklab\yii2\recaptcha\ReCaptcha',
        'siteKey' => 'your site key',
        'secret' => 'your site secret',

7.Now add reCaptcha to your active form like below.

<?= $form->field($subscribe, 'reCaptcha')->widget(
     \himiklab\yii2\recaptcha\ReCaptcha::className())->label(false) ?>

Thats all friends i have implemented recpatcha on my site with this method.Be sure your port 443 is not blocked because Recaptcha use port 443 to connect to google server.Happy coding.

Share Post

comments powered by Disqus