How to use google recaptcha with PHP
step1
first sign up here(google recaptcha api).
The key pair consists of a site key and secret.you can download recaptcha file from google recaptch php library
step2
now you will need to insert this code inside the <form> element where the reCAPTCHA widget will be placed:
require_once('recaptchalib.php'); $publickey = "your_public_key"; // you got this from the signup page echo recaptcha_get_html($publickey);
Example-<html> <body> <!-- the body tag is required or the CAPTCHA may not show on some browsers --> <!-- your HTML content --> <form method="post" action="verify.php"> <?php require_once('recaptchalib.php'); $publickey = "your_public_key"; // you got this from the signup page echo recaptcha_get_html($publickey); ?> <input type="submit" /> </form> <!-- more of your HTML content --> </body> </html>
Step 3
NOw you can check response by this code
<?php
//first i retrieved the recaptcha coming through post method.
//i have used post method, you can use any method you want.
$recaptcha = $_POST['g-recaptcha-response'];
//After retrieving send a post curl request to verify it.
//you will have to send your secret key along with it.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"secret=*******************************=".$recaptcha);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
$server_output = json_decode($server_output,true);
curl_close ($ch);
if($server_output['success']){
// captacha validated successfully.
}else{
// invalid captcha
}
Comments
Post a Comment