简体   繁体   中英

Google Recaptcha box not working. Works if it isn't ticked, but not if it is ticked

This is my enquiry.php file, the code you can see is for the recaptcha box / form, as you'll see I've included my response action, the show_error function outputs if the recaptcha box isn't ticked - this works, but if the box is ticked and the form filled correctly it will not redirect me to the thank-you.html

<?php

$email_to = "billy.farroll@hotmail.com";

if($_POST){

$name;
$telephone;
$email;
$comment;
$captcha;

if(isset($_POST['name']))
$name=$_POST["name"];
if(isset($_POST['telephone']))
$telephone=$_POST["telephone"];
if(isset($_POST['email']))
$email=$_POST["email"];
if(isset($_POST['comment']))
$comment=$_POST["comment"];
if(isset($_POST['g-recaptcha-response']))
$captcha=$_POST['g-recaptcha-response'];



if (empty($name))
{
    show_error("Oops! you forgot to fill in your name, Please go back to the homepage and try again");
}


if (empty($telephone))
{
    show_error("Oops! you forgot to fill in your telephone, Please go back to the homepage and try again");
}


if (empty($email))
{
    show_error("Oops! you forgot to fill in your email, Please go back to the homepage and try again");
}



$email = htmlspecialchars($_POST['email']);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email) || (!$captcha))
{
    show_error("Please tick the box");

}


} else {

    $response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6Lf2sEIUAAAAAMio98GU_V6gki_mGxS3Z6WMBmA9&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
    if($response['success'] == false)

    {

        show_error("Please tick the box");

    }

    else

    {


$email_from = $_POST["email"];
$message = $_POST["message"];
$email_subject = "Enquiry Form";
$headers =
"From: $email_from .\n";
"Reply-To: $email_from .\n";
$message = 
"Name: ". $name . 
"\r\nTelephone Number: " . $telephone . 
"\r\nEmail Address: " . $email . 
 "\r\n\r\n\r\nComments :" . $comment;

ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from);
if ($sent)
{

    header ('location: thank-you.html'); 

        }
    }
}




?>

This is my HTML code for the form, you'll see I've inserted my recaptcha box code below as well as linked to my enquiry.php file

<form id="enquiry-form" action="enquiry.php" style="text-align:left; font-size:10px; margin-top: 20px;" method="post">  
  <fieldset style="border:#FFF; font-size:12px; background-color:#0d5ea9;">


<table width="100%" border="0" cellpadding="0" style="margin-top:15px;">
  <tr>
    <td class="tablebutton">Name:</td>
    <td><input type="text" name="name" id="name" size="50" /></td>
    <td class="tablebutton">Telephone:</td>
    <td><input type="text" name="telephone" id="telephone" size="37" /></td>
  </tr>
  <tr>
    <td class="tablebutton">Email:</td>
    <td> <input type="text" name="email" id="email" size="50" /></td>

  </tr>
</table>

 <div class="hide-for-small-only" style="margin-bottom:10px; font-size: 18px; font-family: 'Saira Semi Condensed', sans-serif; margin-left:10px; color:#FFFFFF; ">Comments</div>
 <div align="left" class="hide-for-small-only" style="margin-left:10px;"><textarea name="comment" id="comment" cols="30" rows="10" style="width:50%; float:left;"></textarea>
 <!--I'm not a robot box -->
 <div class="g-recaptcha" data-sitekey="6Lf2sEIUAAAAAPyFiim58oXAdkgOS-m5dPJd2f1g"></div>
 <!--I'm not a robot box -->
<div class="contactMirror"><strong>Contact Information:</strong><br />
<br>
If you have any other queries regarding the site, it's functionality or any technical  questions about our vinyl service, please do not hesitate to contact us as we are always happy to help. 
<br>
<br>
Please fill in the form and we will get straight back to you.
  </div>
</div>

<br />
<br />  <br />

<div class="submitbutton" align="center" style="float:left; margin-top: 75px;"><button type="submit"><strong>Send</strong></button></div>

 </fieldset>
</form>

I found the answer to this question, please see the amended code for PHP:

$captcha = $_POST["g-recaptcha-response"]; // Declare variable 

if(isset($_POST['g-recaptcha-response'])){
         $captcha=$_POST['g-recaptcha-response'];

    }


if(!$captcha){

    header("Location: <!-- Error HTML page -->");
    exit; 
    }

$secretKey = "<!-- SECRET KEY HERE -->";
$ip = $_SERVER['REMOTE_ADDR'];
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response, true); 
if(intval($responseKeys["success"]) !== 1) {

    echo '<h2>Spam Detected</h2>';

    }

    else {

        header("Location: <!-- Thank you HTML page -->");

        }

//NEW RECAPTCHA

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