添加reCAPTCHA到PHP表单的问题


Issues Adding reCAPTCHA to PHP Form

我最近在PHP中建立了一个联系表单,但是我想添加一个验证码,但是我有点迷路了。我做了一些搜索,似乎谷歌的reCAPTCHA是最好的选择。

我试过从这里附加代码,但提交按钮似乎没有连接。我相信这是一个语法问题,但我不是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>&nbsp;</span><div class="g-recaptcha" data-sitekey="6LcBawsTAAAAAKBPfGs1jApXNRLvR2MIPng0Fxol"></div>
        <label><span>&nbsp;</span><input type="submit" value="" class="submit-button" /></label>                  
    </form>             
</div>
任何帮助或建议将非常感激!谢谢。

首先dump 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');
}

?>