![](/img/trans.png)
[英]Google reCAPTCHA V2 checkbox HTML and PHP Contact Form Code
[英]Google ReCaptcha V2 not working with old PHP Code
我有一个用于组织的小型网站,最近我们的联系页面上充斥着垃圾邮件 email。 我希望添加 Googl ReCapture,但在验证和提交表单时遇到了一些问题。
因此网站会检查字段是否为空,然后允许用户发送消息。 但现在它不会错误检查字段,也不会提交或发送 email。 此外,当您访问联系我们页面时,它会显示消息“感谢您联系...英国,我们会尽快回复您。” 甚至在输入或单击提交之前。
<?php
include ('includes/config.php');
$error = array();
$name = '';
$email = '';
$telephone = '';
$message = '';
//I have blanked my sitekey and secret key out
$siteKey = 'MY_SITE_KEY';
$secretKey = 'MY_SECRET_KEY';
if (isset($_POST['Send']))
{
// Assign form data
$name = $_POST['Name'];
$email = $_POST['Email'];
$telephone = $_POST['Telephone'];
$message = $_POST['Message'];
// Validate reCAPTCHA box
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
// Google reCAPTCHA API secret key
$secretKey = 'Your_reCAPTCHA_Secret_Key';
// Verify the reCAPTCHA response
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secretKey.'&response='.$_POST['g-recaptcha-response']);
// Decode json data
$responseData = json_decode($verifyResponse);
// If reCAPTCHA response is valid
if($responseData->success){
// Check for errors
if (empty($name)) { $error[] = 'Name'; }
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) { $error[] = 'Email'; }
if (empty($telephone)) { $error[] = 'Telephone'; }
if (empty($message)) { $error[] = 'Message'; }
// If no errors
if (sizeof($error) < 1) {
// Build message
$message = "Name: $name \n Email: $email \n Telephone: $telephone \n" . $message;
// Send email
mail('info@MYEMAIL.co.uk', "Message From $name", $message);
// Reset form
$name = '';
$email = '';
$telephone = '';
$message = '';
}
}}}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<?php include ('includes/header.php'); ?>
<div id="wrapper">
<div id="main">
<h1>Contact Us</h1>
<?php
if (sizeof($error) > 0)
{
echo '<p>There were errors for the following fields:</p><ul>';
foreach ($error as $val) {
echo "<li>$val</li>";
}
echo '</ul>';
}
else
{
echo '<p>Thank you for contacting ... UK, we will get back to you shortly.</p>';
}
?>
<table cellpadding="5" cellspacing="0">
<form method="post" action="">
<tr>
<td width="150"><label for="Name">Name: </label></td>
<td><input type="text" name="Name" value="<?=$name?>" /></td>
</tr>
<tr>
<td><label for="Email">Email: </label></td>
<td><input type="text" name="Email" value="<?=$email?>" /></td>
</tr>
<tr>
<td><label for="Telephone">Telephone: </label></td>
<td><input type="text" name="Telephone" value="<?=$telephone?>" /></td>
</tr>
<tr>
<td valign="top"><label for="Message">Message: </label></td>
<td><textarea name="Message" rows="10" cols="55"><?=$message?></textarea></td>
</tr>
<tr>
<div class="g-recaptcha" data-sitekey="<?php echo $siteKey; ?>"></div>
</tr>
<tr>
<td></td>
<td><button type="submit" name="Send">Send Message</button></td>
</tr>
</form>
</table>
</div>
<?php include ('includes/footer.php'); ?>
</div>
</body>
</html>
我需要它做的是检查字段是否正确并且 ReCapture 已经过验证,并且当用户单击提交时,会发送 email。
我就是这样做的,以一种工作的方式。 首先,我的页面显示了recaptcha。 我没有任何表单字段,因为此应用程序不需要它们,但您可以轻松添加它们:
<?php
$mailid = $_GET['mailid']; // which email address is needed?
?>
<html>
<head>
<title>My test contact verification</title>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" crossorigin="anonymous"></script>
</head>
<body>
<h1>Please confirm you are not a robot</h1>
<form action="?" method="POST">
<input type="hidden" name="mailid" id="mailid" value="<?php echo $mailid;?>">
<div class="g-recaptcha" data-sitekey="my-site-key" data-callback="cbtest"></div>
<br/>
</form>
<br />
<br />
<div id="result"></div>
<script type="text/javascript">
function cbtest(response) {
// now, send the response to be verified
var dispdiv = document.getElementById("result");
dispdiv.innerHTML = "<p>Retrieving the contact details</p>";
var contact = document.getElementById("mailid");
var contactsel = contact.value;
$.ajax({
url: "http://www.example.com/recaptcha_verify.php",
type: "POST",
data: {source: contactsel, response: response},
}).done(function(data, status, xhr) {
var ver = data.substring(0,2);
if (ver == "no") {
dispdiv.innerHTML = "<p>Sorry, there was a verification problem.</p>";
} else if (ver == "OK") {
var out = data.substring(3);
dispdiv.innerHTML = "<p>The contact details you requested are:<br />" + out + "</p>";
} // end of verification response check
})
};
</script>
</body>
</html>
和我的验证码
<?php
if (isset($_POST['response'])) {
$response = $_POST['response']; }
else {
echo "no1";
exit;
}
if (isset($_POST['source'])) {
$source = $_POST['source']; }
else {
echo "no2";
exit;
}
$url = "https://www.google.com/recaptcha/api/siteverify";
$secret = "my-secret";
$emails = array(
"hidden email 1",
"hidden email 2",
"hidden email 3");
// is the source reference a valid one?
if ($source < 0 || $source > count($emails)) {
echo "no";
exit;
}
// create the output
$url = $url."?secret=".$secret. "&response=".$response;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, TRUE);
$curlData = curl_exec($curl);
curl_close($curl);
$out = $emails[$source];
// send the response to the verifier
$captcha_success = json_decode($curlData, TRUE);
if ($captcha_success['success'] == false) {
echo "noF";
}
else if ($captcha_success['success'] == true) {
echo "OK|" . $out;
}
?>
(我已经从上面删除了我认为是虚假代码的内容,希望我没有采取太多措施)。
我向用户展示了一个只有文本和一个recaptcha 的屏幕,传入的变量被传递以告诉我的代码需要哪个隐藏的email 地址,显然您将添加表单字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.