Recently Google introduced the new and improved reCaptcha API called Are you a robot?. I am impressed with the makeover that reCaptcha has got. Its has become simple, user friendly and protects your websites for spammers and robots. This tutorial will show you how new reCaptcha can be setup and integrated into a PHP code.
Captcha present on most of the websites out there has frustrated users everywhere. Recently many new user friendly captcha solutions have been developed but I think best of them will be the Google’s new reCaptcha. Here is how you can integrate new reCaptcha in PHP.
Click here to create a Google reCaptcha application.
Register your website
Enter your website domain details excluding http or https
reCaptcha Keys
Here are the two keys generated for your domain. Site Key will be used in HTML client side code and Secret Key will be used in your PHP server-side code for communication with Google using API.
HTML Code
This contains simple HTML code with reCaptcha snippet. Modify the Google Site Key in below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<html> <head> /* Google new reCaptcha JS */ <script src="https://www.google.com/recaptcha/api.js"></script> </head> <body> <form action="" method="post"> Username: <input type="text" name="username" class="input" /> Password: <input type="password" name="password" class="input" /> <div class="g-recaptcha" data-sitekey="Google Site Key"></div> <input type="submit" value="Log In" /> <span class='alert'><?php echo $alert; ?></span> </form> </body> </html> |
inc.php
Enter reCaptcha secret and site key in this file. Also a function is created to CURL and get reCaptcha response.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?PHP $site_key = "Enter Site Key Here"; $secret_key = "Enter Secret Key Here"; define('DB_SERVER', 'localhost'); define('DB_USERNAME', 'username'); define('DB_PASSWORD', 'password'); define('DB_DATABASE', 'database'); $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE); function get_recaptcha_response($response){ global $secret_key; $google_url="https://www.google.com/recaptcha/api/siteverify"; $ip=$_SERVER['REMOTE_ADDR']; $url=$google_url."?secret=".$secret_key."&response=".$response."&remoteip=".$ip; $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"); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $curlData = curl_exec($curl); // Check if any error occurred if(curl_errno($curl)) { echo 'Curl error: ' . curl_error($curl); } curl_close($curl); $result = json_decode($curlData, true); return $result; } ?> |
example.php
This file includes the above HTML code and PHP code to verify reCaptcha response, username and password.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
include("inc.php"); session_start(); $alert=''; if($_SERVER["REQUEST_METHOD"] == "POST"){ $response=$_POST['g-recaptcha-response']; if(!empty($response)){ $result = get_recaptcha_response($response); if($result['success']){ /*Recaptcha verified. Enter code here*/ //Code for login information verification /* $username=mysqli_real_escape_string($db,$_POST['username']); $password=md5(mysqli_real_escape_string($db,$_POST['password'])); if(!empty($username) && !empty($password)) { $result=mysqli_query($db,"SELECT id FROM users WHERE username='$username' and passcode='$password'"); $row=mysqli_fetch_array($result,MYSQLI_ASSOC); if(mysqli_num_rows($result)==1) { $_SESSION['login_user']=$username; header("location: admin_panel.php"); } else { $alert="Username or Password invalid. Please enter again"; } } else { $alert="Username or Password invalid. Please enter again"; } */ // Code end for login information verification $alert = "reCaptcha Verified."; } else { $alert="Please re-enter your reCAPTCHA."; } } else { $alert="Please re-enter your reCAPTCHA."; } } ?> |
Google’s New and easy reCaptcha PHP integration - www.earlysandwich.com.zip
I see that is recaptcha v2. thanks , nice tutorial 🙂