reCAPTCHA 2.0

So I’ve been using reCAPTCHA newer version (2.0) with yii and I haven’t found any discussion to this topic yet, instead of the older version where you can find it in reCAPTCHA 1.3 Extension.

reCAPTCHA is a free service provided by Google that protects your site from spam and abuse. The reCAPTCHA 2.0 is the newer version of reCAPTCHA developed by Google.

In this version, we don’t have to download any extension code or module. We can easily render the widget by calling the google reCAPTCHA api.js in our view.

To use it in your site, you need to have valid site key and secret key for it. You can get them by registering yourself at https://www.google.com/recaptcha

Current Version: 2.0

Resources

  • reCAPTCHA

  • Join Discussion

[size="5"]Documentation[/size]

Requirements

  • Yii 1.1 or above

Usage

Make sure you have your site key and secret key from Google reCAPTCHA

The easiest method for rendering the reCAPTCHA widget on your page is to include the necessary JavaScript resource and a g-recaptcha tag. The g-recaptcha tag is a DIV element with class name ‘g-recaptcha’ and your site key in the data-sitekey attribute.

In the view:

Put this script code before/after your form:




<script src="https://www.google.com/recaptcha/api.js" async defer></script>



It will automatically calls the JavaScript from Google through the internet.

Next, to display the widget you have to put this code inside your form:


<form>

..

<div class="g-recaptcha" data-sitekey="your_site_key"></div>

..

</form>

Your widget will be displayed when you open your page.

If you want to explicitly render the widget (to use callback etc) you can find the documentation provided by Google in this link

I have used this extension, works fine.

Thanks Yuri, for pointing us to the reCaptcha API 2 extension (https://github.com/dakiquang/yiiReCaptcha).

It looks very promising but we cannot get it to work running Yii1. The error we get on a Mac running MAMP is:

Fatal error: Cannot redeclare class ReCaptcha in /Users/buddy/Documents/MAMPROOT/mysite.com/protected/extensions/yiiReCaptcha/ReCaptcha.php on line 294

This is in the config main.php

    'reCaptcha' =&gt; array(


            'name' =&gt; 'reCaptcha',


            'class' =&gt; 'application&#092;extensions&#092;yiiReCaptcha&#092;ReCaptcha',


            'key' =&gt; '&lt;our_key&gt;',


            'secret' =&gt; '&lt;our_secret&gt;',


        ),

This is in the formModel:

public function rules()


{


	return array(


		array('verifyCode', 'required'),


		array('verifyCode', 'application.extensions.yiiReCaptcha.ReCaptchaValidator'),


	);


}

And the view:

<?php

$this->widget(‘application.extensions.yiiReCaptcha.ReCaptcha’, array(

'model'     =&gt; &#036;model,


'attribute' =&gt; 'verifyCode',

));

?>

Any suggestions are very welcome indeed!

Cheers,

Giordano