Enterprise System Developer

Google reCaptcha using CakePHP - Are you a Robot?

Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Email this to someonePrint this page

Google has been introduced new reCaptcha API which is Are you a robot? A new design captcha system. This protects your website for robots and spammers, in this post I had implemented new reCaptch API system with HTML login form using CakePHP. Please take a look quick look at the demo.

recaptcha

Get reCaptcha Key
Click here to create a Google reCaptcha application.

Register Your Website
Give your website domain details without http:

register

Google Site Key
You will use this in HTML code.

sitekey

Google Secret Key
This will help your website to communication with Google.

secretkey

HTML code
Contains HTML code with Google reCaptcha snippet. You have to modify the GOOGLE_SITE_KEY value.

<html>
<head>
/* Google reCaptcha JS */
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<form action="" method="post">
<label>Username</label>
<?php echo $this->Form->text('User.username', array('maxlength' => 32))?>
<label>Password</label>
<?php echo $this->Form->password('User.password', array('maxlength' => 32))?>
<div class="g-recaptcha" data-sitekey="GOOGLE_SITE_KEY"></div>
<input type="submit"  value="Log In" />
</form>
</body>
</html>

Create new Vendor in Vendor folder:

curl.php
<?php
function getCurlData($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
$curlData = curl_exec($curl);
curl_close($curl);
return $curlData;
}
?>

Using in controller, You have to modify the GOOGLE_SECRET_KEY value.

$recaptcha = $this->data['g-recaptcha-response'];

$google_url = "https://www.google.com/recaptcha/api/siteverify";
$secret = 'GOOGLE_SECRET_KEY';
$ip = $_SERVER['REMOTE_ADDR'];
$url = $google_url . "?secret=" . $secret . "&response=" . $recaptcha ."&remoteip=" . $ip;
App::import('Vendor', 'curl');
$res = getCurlData($url);
$res = json_decode($res, true);

if(empty($res['success'])){

//if success not empty
//some code here
}

Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Email this to someonePrint this page

12 Responses

  1. Eduardo says:

    Thanks for the article.

  2. Johne202 says:

    An interesting dialogue is worth comment. I feel that you must write more on this topic, it won’t be a taboo subject but typically people are not sufficient to speak on such topics. To the next. Cheers dkeekabccdce

  3. Johnd594 says:

    hi and thanks for the actual blog post ive recently been searching regarding this specific advice online for sum hours these days as a result thanks bdddkckfbdbd

  4. Johnk506 says:

    I think you have observed some very interesting details , appreciate it for the post. eecgdgafacef

  5. corburterilio says:

    I have been browsing on-line greater than three hours lately, but I never found any fascinating article like yours. It¦s pretty value sufficient for me. Personally, if all web owners and bloggers made good content as you probably did, the net might be much more helpful than ever before.

Comments

Your email address will not be published. Required fields are marked *