How to validate google captcha response PHP

How to validate google captcha response PHP



So in this article we are going to check the response coming from google captcha. Previously we added google captcha using PHP. If you have’t seen that article check it out Here Is The Article





So to make google captcha work we add some code in to the HTML FORM that we use and also include a google link to the head of our website
,I have already explained it in Here Is The Article.



so once the captcha is running now we want to know that if it is validating or not.
just for example look at below given HTML FORM.





     		

This HTML FORM looks like this







now to validate we get the value from this HTML FORM using PHP. As givin



     if(isset($_POST['data'])){
		//if image is set
		if(isset($_FILES['image'])){
			//if the google captcha is set or not 
			if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
				$secret = '6LfLzZcUAAAAALAmqYBHi2_6GlCll_6ipOoAWb20';

				$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
				//getting response from google
				$responseData = json_decode($verifyResponse);

				if($responseData->success){
					echo $_FILES['image']['name'];
				}
				//echo"
";
				//print_r($responseData);



			}
		}
	}

Now in above PHP code we are checking that if "SUBMIT" button is pressed and user also have selected any "image" to upload.Have user clicked on the captcha .





$_POST['g-recaptcha-response'] is the variable which holding the response

We also have Secret key by using it we are getting response from "'https://www.google.com/recaptcha/api/siteverify".
And then putting it into a variable name "ResponseData".If this variable has some value. Then we are getting something on the screen using "echo".



So finally it is validated you can use above code with your code to validate your "Google captcha response"