PHP 表单、MailFonction 和 Google reCaptcha 验证问题
PHP Form, MailFonction and Google reCaptcha Validation Issue
我对使用邮件功能编写验证码验证程序的方式有疑问。
Captcha Success 运行良好。当验证码失败时,我在表单页面中收到了正确的消息,但是 PHP 程序仍然让电子邮件发送。我只需要知道如何只在成功部分设置邮件功能。
谁能帮我解决 reCaptcha 验证问题?
谢谢。
这里是 PHP 源代码:
<?php
if (isset($_POST["submit"])) {
$url = 'https://www.google.com/recaptcha/api/siteverify';
$privatekey = "MASKED";
$response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response);
if(isset($data->success) AND $data->success==true){
header('Location: appel_de_service.php?CaptchaPass=True');
}else{
header('Location: appel_de_service.php?CaptchaFail=True');
}
$to = "MASKED";
$from = $_POST['courriel'];
$from_name = $_POST['nom_responsable'];
$subject = "Reception d'un appel de service ";
$nom_compagnie = $_POST['nom_compagnie']; // required
$adresse = $_POST['adresse']; // required
$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']; // required
$coordonnees = $_POST['coordonnees']; // required
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "Reply-to: $courriel";
$message = "
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<title>Appel de service (rempli sur le site internet)</title>
<style type=\"text/css\">
h1,h2,h3,h4,h5,h6 {
font-family: Cambria, \"Hoefler Text\", \"Liberation Serif\", Times, \"Times New Roman\", serif;
color: #272727;
}
</style>
</head>
<body>
<h2 style=\"font-size: 1.25em; font-family: Gotham, ʼHelvetica Neueʼ, Helvetica, Arial, sans-serif;\">Vous avez reçu une demande d'appel de service en ligne.</h2>
<table width=\"500\" border=\"1\" cellpadding=\"5\" cellspacing=\"2\">
<tbody>
<tr>
<td colspan=\"2\" align=\"left\" valign=\"middle\" bgcolor=\"#84BDEC\"><h3>Informations sur l'adresse du service</h3></td>
</tr>
<tr>
<td width=\"155\" align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Nom de la compagnie</td>
<td width=\"313\" align=\"left\" valign=\"middle\">$nom_compagnie</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Adresse</td>
<td align=\"left\" valign=\"middle\">$adresse</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Ville</td>
<td align=\"left\" valign=\"middle\">$ville</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Province</td>
<td align=\"left\" valign=\"middle\">$province</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Code postale</td>
<td align=\"left\" valign=\"middle\">$code_postale</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Nom du responsable</td>
<td align=\"left\" valign=\"middle\">$nom_responsable</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Téléphone</td>
<td align=\"left\" valign=\"middle\">$telephone</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Adresse courriel</td>
<td align=\"left\" valign=\"middle\">$courriel</td>
</tr>
</tbody>
</table>
<br>
<table width=\"500\" border=\"1\" cellpadding=\"5\" cellspacing=\"2\">
<tbody>
<tr>
<td colspan=\"2\" align=\"left\" valign=\"middle\" bgcolor=\"#84BDEC\"><h3>Informations sur le produit</h3></td>
</tr>
<tr>
<td width=\"155\" align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Marque</td>
<td width=\"313\" align=\"left\" valign=\"middle\">$marque</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Numéro de modèle</td>
<td align=\"left\" valign=\"middle\">$numero_modele</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Garantie</td>
<td align=\"left\" valign=\"middle\">$garantie</td>
</tr>
<tr bgcolor=\"#D5D5D5\">
<td colspan=\"2\" align=\"left\" valign=\"middle\">Description du problème :</td>
</tr>
<tr>
<td height=\"75\" colspan=\"2\" align=\"left\" valign=\"top\">$description</td>
</tr>
</tbody>
</table>
<br>
<table width=\"500\" border=\"1\" cellpadding=\"5\" cellspacing=\"2\">
<tbody>
<tr>
<td colspan=\"2\" align=\"left\" valign=\"middle\" bgcolor=\"#84BDEC\"><h3>Préférences pour le rendez-vous</h3></td>
</tr>
<tr>
<td width=\"155\" align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Disponibilité</td>
<td width=\"313\" align=\"left\" valign=\"middle\">$disponibilite</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Coordonnées</td>
<td align=\"left\" valign=\"middle\">$coordonnees</td>
</tr>
</tbody>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
mail($to, $subject, $message, $headers);
}
?>
出现此问题的原因可能有很多。 reCpatcha Google 插件失败的最常见原因是用户通过 ajax 和 jquery 或 javascript 和 php 进行验证的方式是错误的。我将向您展示的下一个代码(假设您已经在 Goolge Recaptcha 中验证了一个帐户),将教您如何在单击带有 php、ajax 的按钮后验证 reCaptcha 答案和 jquery。您好!
从这里下载 recpatcha 库 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
?>
已解决。问题是 PHP if() 和 else() 的构造需要在数据收集填写到表单之后。
正确的代码如下:
<?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');
}
?>
我对使用邮件功能编写验证码验证程序的方式有疑问。
Captcha Success 运行良好。当验证码失败时,我在表单页面中收到了正确的消息,但是 PHP 程序仍然让电子邮件发送。我只需要知道如何只在成功部分设置邮件功能。
谁能帮我解决 reCaptcha 验证问题?
谢谢。
这里是 PHP 源代码:
<?php
if (isset($_POST["submit"])) {
$url = 'https://www.google.com/recaptcha/api/siteverify';
$privatekey = "MASKED";
$response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response);
if(isset($data->success) AND $data->success==true){
header('Location: appel_de_service.php?CaptchaPass=True');
}else{
header('Location: appel_de_service.php?CaptchaFail=True');
}
$to = "MASKED";
$from = $_POST['courriel'];
$from_name = $_POST['nom_responsable'];
$subject = "Reception d'un appel de service ";
$nom_compagnie = $_POST['nom_compagnie']; // required
$adresse = $_POST['adresse']; // required
$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']; // required
$coordonnees = $_POST['coordonnees']; // required
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "Reply-to: $courriel";
$message = "
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<title>Appel de service (rempli sur le site internet)</title>
<style type=\"text/css\">
h1,h2,h3,h4,h5,h6 {
font-family: Cambria, \"Hoefler Text\", \"Liberation Serif\", Times, \"Times New Roman\", serif;
color: #272727;
}
</style>
</head>
<body>
<h2 style=\"font-size: 1.25em; font-family: Gotham, ʼHelvetica Neueʼ, Helvetica, Arial, sans-serif;\">Vous avez reçu une demande d'appel de service en ligne.</h2>
<table width=\"500\" border=\"1\" cellpadding=\"5\" cellspacing=\"2\">
<tbody>
<tr>
<td colspan=\"2\" align=\"left\" valign=\"middle\" bgcolor=\"#84BDEC\"><h3>Informations sur l'adresse du service</h3></td>
</tr>
<tr>
<td width=\"155\" align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Nom de la compagnie</td>
<td width=\"313\" align=\"left\" valign=\"middle\">$nom_compagnie</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Adresse</td>
<td align=\"left\" valign=\"middle\">$adresse</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Ville</td>
<td align=\"left\" valign=\"middle\">$ville</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Province</td>
<td align=\"left\" valign=\"middle\">$province</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Code postale</td>
<td align=\"left\" valign=\"middle\">$code_postale</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Nom du responsable</td>
<td align=\"left\" valign=\"middle\">$nom_responsable</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Téléphone</td>
<td align=\"left\" valign=\"middle\">$telephone</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Adresse courriel</td>
<td align=\"left\" valign=\"middle\">$courriel</td>
</tr>
</tbody>
</table>
<br>
<table width=\"500\" border=\"1\" cellpadding=\"5\" cellspacing=\"2\">
<tbody>
<tr>
<td colspan=\"2\" align=\"left\" valign=\"middle\" bgcolor=\"#84BDEC\"><h3>Informations sur le produit</h3></td>
</tr>
<tr>
<td width=\"155\" align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Marque</td>
<td width=\"313\" align=\"left\" valign=\"middle\">$marque</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Numéro de modèle</td>
<td align=\"left\" valign=\"middle\">$numero_modele</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Garantie</td>
<td align=\"left\" valign=\"middle\">$garantie</td>
</tr>
<tr bgcolor=\"#D5D5D5\">
<td colspan=\"2\" align=\"left\" valign=\"middle\">Description du problème :</td>
</tr>
<tr>
<td height=\"75\" colspan=\"2\" align=\"left\" valign=\"top\">$description</td>
</tr>
</tbody>
</table>
<br>
<table width=\"500\" border=\"1\" cellpadding=\"5\" cellspacing=\"2\">
<tbody>
<tr>
<td colspan=\"2\" align=\"left\" valign=\"middle\" bgcolor=\"#84BDEC\"><h3>Préférences pour le rendez-vous</h3></td>
</tr>
<tr>
<td width=\"155\" align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Disponibilité</td>
<td width=\"313\" align=\"left\" valign=\"middle\">$disponibilite</td>
</tr>
<tr>
<td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Coordonnées</td>
<td align=\"left\" valign=\"middle\">$coordonnees</td>
</tr>
</tbody>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
mail($to, $subject, $message, $headers);
}
?>
出现此问题的原因可能有很多。 reCpatcha Google 插件失败的最常见原因是用户通过 ajax 和 jquery 或 javascript 和 php 进行验证的方式是错误的。我将向您展示的下一个代码(假设您已经在 Goolge Recaptcha 中验证了一个帐户),将教您如何在单击带有 php、ajax 的按钮后验证 reCaptcha 答案和 jquery。您好!
从这里下载 recpatcha 库 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
?>
已解决。问题是 PHP if() 和 else() 的构造需要在数据收集填写到表单之后。
正确的代码如下:
<?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');
}
?>