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.
Get reCaptcha Key
Click here to create a Google reCaptcha application.
Register Your Website
Give your website domain details without http:
Google Site Key
You will use this in HTML code.
Google Secret Key
This will help your website to communication with Google.
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 }
12 Responses
Thanks for the article.
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
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
I think you have observed some very interesting details , appreciate it for the post. eecgdgafacef
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.