将 reCAPTCHA 添加到 PHP 表单的问题
Issues Adding reCAPTCHA to PHP Form
我最近在 PHP 中建立了一个联系表单,但我想添加一个验证码,但我有点迷路了。我进行了一些搜索,结果似乎 Google 的 reCAPTCHA 是最佳选择。
我已经尝试附加来自 here 的代码,但提交按钮似乎没有连接。我认为这是语法问题,但我不是 PHP 专业人士。
PHP:
<?php
// Load reCAPTCHA library
require_once "recaptchalib.php";
$name = Trim(stripslashes($_POST['name']));
$email = Trim(stripslashes($_POST['email']));
$message = Trim(stripslashes($_POST['message']));
$emailFrom = $email;
$emailTo = "mario@mtscollective.com";
$subject = "API Website Contact Request";
// Prepare email body text
$body = "<strong>Name:</strong> $name <br /> <strong>Email:</strong> $email <br /> <strong>Message:</strong> $message";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $name <$emailFrom>" . "\r\n";
foreach ($_POST as $key => $value) {
echo '<p><strong>' . $key.':</strong> '.$value.'</p>';
}
$secret = "xxx";
$response = null;
$reCaptcha = new ReCaptcha($secret);
// If submitted, check response
if ($_POST["g-recaptcha-response"]) {
$response = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]
);
}
// Send email
$success = mail($emailTo, $subject, $body, $headers);
// Redirect to success or error pages
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=thankyou.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>
HTML:
<div class="contact-form">
<form role="form" method="post" action="contact-form.php">
<label for="name"><span>Name</span><input type="text" class="input-field" name="name" required data-errormessage-value-missing="Please enter your name." /></label>
<label for="email"><span>Email</span><input type="email" class="input-field" name="email" required data-errormessage-value-missing="Please enter your email address." /></label>
<label for="message"><span>Message</span><textarea name="message" class="textarea-field" required data-errormessage-value-missing="Please enter your message."></textarea></label>
<span> </span><div class="g-recaptcha" data-sitekey="6LcBawsTAAAAAKBPfGs1jApXNRLvR2MIPng0Fxol"></div>
<label><span> </span><input type="submit" value="" class="submit-button" /></label>
</form>
</div>
如有任何帮助或建议,我们将不胜感激!谢谢
首先转储recaptcha版本1开始使用版本2
https://developers.google.com/recaptcha/intro
安装后:
<?php
// Load reCAPTCHA library
include_once ("YOURPATH/reCAPTCHA/autoload.php");
$name = Trim(stripslashes($_POST['name']));
$email = Trim(stripslashes($_POST['email']));
$message = Trim(stripslashes($_POST['message']));
$emailFrom = $email;
$emailTo = "mario@mtscollective.com";
$subject = "API Website Contact Request";
// Prepare email body text
$body = "<strong>Name:</strong> $name <br /> <strong>Email:</strong> $email <br /> <strong>Message:</strong> $message";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $name <$emailFrom>" . "\r\n";
/*
* foreach ($_POST as $key=>$value){
* echo '<p><strong>' . $key . ':</strong> ' . $value . '</p>';
* }
*/
$secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'],$_SERVER['REMOTE_ADDR']);
if ($resp->isSuccess()){
$success = mail($emailTo,$subject,$body,$headers);
header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . 'thankyou.html');
}else{
header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . 'error.html');
}
?>
我最近在 PHP 中建立了一个联系表单,但我想添加一个验证码,但我有点迷路了。我进行了一些搜索,结果似乎 Google 的 reCAPTCHA 是最佳选择。
我已经尝试附加来自 here 的代码,但提交按钮似乎没有连接。我认为这是语法问题,但我不是 PHP 专业人士。
PHP:
<?php
// Load reCAPTCHA library
require_once "recaptchalib.php";
$name = Trim(stripslashes($_POST['name']));
$email = Trim(stripslashes($_POST['email']));
$message = Trim(stripslashes($_POST['message']));
$emailFrom = $email;
$emailTo = "mario@mtscollective.com";
$subject = "API Website Contact Request";
// Prepare email body text
$body = "<strong>Name:</strong> $name <br /> <strong>Email:</strong> $email <br /> <strong>Message:</strong> $message";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $name <$emailFrom>" . "\r\n";
foreach ($_POST as $key => $value) {
echo '<p><strong>' . $key.':</strong> '.$value.'</p>';
}
$secret = "xxx";
$response = null;
$reCaptcha = new ReCaptcha($secret);
// If submitted, check response
if ($_POST["g-recaptcha-response"]) {
$response = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]
);
}
// Send email
$success = mail($emailTo, $subject, $body, $headers);
// Redirect to success or error pages
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=thankyou.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>
HTML:
<div class="contact-form">
<form role="form" method="post" action="contact-form.php">
<label for="name"><span>Name</span><input type="text" class="input-field" name="name" required data-errormessage-value-missing="Please enter your name." /></label>
<label for="email"><span>Email</span><input type="email" class="input-field" name="email" required data-errormessage-value-missing="Please enter your email address." /></label>
<label for="message"><span>Message</span><textarea name="message" class="textarea-field" required data-errormessage-value-missing="Please enter your message."></textarea></label>
<span> </span><div class="g-recaptcha" data-sitekey="6LcBawsTAAAAAKBPfGs1jApXNRLvR2MIPng0Fxol"></div>
<label><span> </span><input type="submit" value="" class="submit-button" /></label>
</form>
</div>
如有任何帮助或建议,我们将不胜感激!谢谢
首先转储recaptcha版本1开始使用版本2
https://developers.google.com/recaptcha/intro
安装后:
<?php
// Load reCAPTCHA library
include_once ("YOURPATH/reCAPTCHA/autoload.php");
$name = Trim(stripslashes($_POST['name']));
$email = Trim(stripslashes($_POST['email']));
$message = Trim(stripslashes($_POST['message']));
$emailFrom = $email;
$emailTo = "mario@mtscollective.com";
$subject = "API Website Contact Request";
// Prepare email body text
$body = "<strong>Name:</strong> $name <br /> <strong>Email:</strong> $email <br /> <strong>Message:</strong> $message";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $name <$emailFrom>" . "\r\n";
/*
* foreach ($_POST as $key=>$value){
* echo '<p><strong>' . $key . ':</strong> ' . $value . '</p>';
* }
*/
$secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'],$_SERVER['REMOTE_ADDR']);
if ($resp->isSuccess()){
$success = mail($emailTo,$subject,$body,$headers);
header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . 'thankyou.html');
}else{
header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . 'error.html');
}
?>