简体   繁体   中英

Issues Adding reCAPTCHA to PHP Form

I recently built a contact form in PHP, but I'd like to add a captcha, however I'm a little lost. I've done some searching, and it seams as though Google's reCAPTCHA is the best option.

I've tried appending the code from here , but the submit button doesn't seem to be connected. I believe it's a syntax issue, but I'm not a PHP pro.

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>

Any help or advice would be really appreciated! Thanks.

firstly dump recaptcha version 1 start using version 2

https://developers.google.com/recaptcha/intro

once installed:

<?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');
}


?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM