PHP Form, MailFonction And Google ReCaptcha Validation Issue
I have a issue with the way ive program the captcha verification with the mail fonction. The Captcha Success are working fine. When the captcha fail, i got the proper message into
Solution 1:
The issue could be for many reasons. The most popular reasons thats failed the reCpatcha Google Plugin is because the way that the user validate trough ajax with jquery or javascript and php are wrong. The next code that I will show you (assuming that you already have verified an account in Goolge Recaptcha), will teach you how to validate a reCaptcha answer after you are click in a button with php, ajax and jquery. Greetings!
Download the recpatcha library from here https://mega.nz/#!qw4Snb6Z!3Mgq2UvD3PmQJ9ts9sQdHcV86Le8-wtz05IEr2b-3mw
<html>
<head>
<title>Example</title>
</head>
<script src='https://www.google.com/recaptcha/api.js'></script>
<script>
//This is our DOM on JQUERY or JAVASCRIPT named as main.js
$(function() {
//First we have to listen on a event click (button) and extract the values of the inputs
$('body').on('click','.myButtonClass', function() {
$email = $('#emailID').val();
$password = $('#passwordID').val();
$recaptcha = $('textarea').val();
//We save in a object our values
$x = {
action:'validate',
email:$email,
password:$password
captcha:$recaptcha
};
//We execute ajax calling a php file for POST method
$.post('file.php',$x,function(callback) {
$('.answer').html(callback);
});
});
});
/*END OF DOM AND MAIN.JS*/
</script>
<body>
<form>
<input type="email" id="emailID" placeholder="Write here your email">
<input type="password" id="passwordID" placeholder="Write here your password">
<input type="button" class="myButtonClass">
</form>
<!--Your div recaptcha. This give you the page recaptcha google when you are into your configuration Change the XXXX for your SITE KEY numbers and letters -->
<div class="g-recaptcha" data-sitekey="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"></div>
<!--This will be where we display the answer with ajax, in the element who have the class answer-->
<div class="answer"></div>
</body>
</html>
<?php
//this is your file.php
require_once 'recaptchalib.php';
//BEGIN OUR PHP FILE WHERE WE'LL RECIEVE ALL THE DATA FROM THE AJAX FILE
//we save the action sended trough ajax
$action = $_POST['action'];
if ( $action == 'validate' ) :
//WE SAVE A EMAIL
$email = addslashes($_POST['email']);
//WE SAVE THE PASSWORD FOR THIS EXAMPLE
$password = addslashes($_POST['password']);
//We paste here one more time our secret key
$secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$response = null;
$reCaptcha = new ReCaptcha($secret);
$captcha = $_POST["captcha"];
//WE VALIDATE IF THE INPUTS ARE CORRECTLY INPUT
$input = strlen($email)*strlen($password);
if ( $_POST["captcha"] ) :
$response = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$captcha
);
endif;
//We validate if exists a response success from google recaptcha and if is it, we continue
if ( $response != null && $response->success ) :
//if the inputs were in
if ( $input > 0 ) :
echo 'OWW YEAH, YOU VALIDATE CORRECTLY GOOGLE RECAPTCHA AND YOUR EMAIL IS '.$email.' AND YOUR PASSWORD IS: '.$password;
else :
echo 'Sorry but you are not fill all the inputs. All are required, please refresh the page and try it again';
endif;
else :
echo 'Hey, man! Chillout, first validate the google recpatcha and fill the inputs and click the button for continue. Thanks';
endif;
endif;
/*END OF PHP FILE*/
//BEGIN OUR FORM IN HTML
?>
Solution 2:
Resolved. The problem was the construction of the PHP if() and else() need to be after the Data collection filled into the form.
Here are the proper code :
<?php
if (isset($_POST["submit"])) {
$url = 'https://www.google.com/recaptcha/api/siteverify';
$privatekey = "PRIVATE_KEY";
$response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']);
$data = json_decode($response);
if(isset($data->success) AND $data->success==true){
$to = "who@domain.com";
$from = $_POST['courriel'];
$from_name = $_POST['nom_responsable'];
$subject = "Reception d'un appel de service ";
$nom_compagnie = $_POST['nom_compagnie'];
$adresse = $_POST['adresse'];
$ville = $_POST['ville'];
$province = $_POST['province'];
$code_postale = $_POST['code_postale'];
$nom_responsable = $_POST['nom_responsable'];
$courriel = $_POST['courriel'];
$telephone = $_POST['telephone'];
$marque = $_POST['marque'];
$numero_modele = $_POST['numero_modele'];
$garantie = $_POST['garantie'];
$description = $_POST['description'];
$disponibilite = $_POST['disponibilite'];
$coordonnees = $_POST['coordonnees'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "Reply-to: $courriel";
$message = "
";
if(mail($to, $subject, $message, $headers)){
header('Location: appel_de_service.php?CaptchaPass');
}else{
echo "mail could not be sent";
}
}else{
header('Location: appel_de_service.php?CaptchaFail');
}
}else{
header('Location: appel_de_service.php?CaptchaError');
}
?>
Post a Comment for "PHP Form, MailFonction And Google ReCaptcha Validation Issue"