stumped with recaptcha verification step

Viewed 43

I hate asking for help.. but I've spent I dunno how many hours trying to figure out how to implement the verification step for this form with recaptcha.

I understand the I need define the private key somewhere, but most of the other guides are using "isset" functions and I just don't at which part of the php to do it.

At the moment, the button shows up and is checkable.. but when I click submit, it sends regardless of whether the box was checked. (Just explaining in case that's helpful.)

Thanks, sorry.

<?php

$to = 'example@caraudioinc.com';
$subject = 'CarAudioInc.com Form Message';

$errName = $errEmail = $errMessage = "";
$name = $email = $pNum = $message = "";


if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = test_input($_POST["name"]);
  $email = test_input($_POST["email"]);
  $pNum = test_input($_POST["pNum"]);
  $message = test_input($_POST["message"]);

}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if (empty($_POST["name"])) {
    $errName = "*Name field is required.";
    } else {
    $name = test_input($_POST["name"]);

        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $errName = "Invaild name."; 
        }
    }

    if (empty($_POST["email"])) {
    $errEmail = "*Email field is required.";
        } else {
    $email = test_input($_POST["email"]);

        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid E-mail."; 
        }
    }

    if (empty($_POST["message"])) {
    $errMessage = "*Message field is required.";
        } else {
    $message = test_input($_POST["message"]);
    }

$body = <<<EMAIL

Message from: $name

$message


Phone: $pNum
Email: $email

EMAIL;

$header = "From: $email";   

if (!$errName && !$errEmail && !$errMessage ) {
    if($_POST) {



        mail($to, $subject, $body, $header);

        header('Location: /sent');
    }

    }
}
?>


<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=1152">
<script src="https://www.google.com/recaptcha/api.js"></script>


<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-100617570-1" ></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-100617570-1');
</script>

<!--Google meta tags -->
<title>Car Audio Inc | Contact</title>

<!--fb meta tags -->
<link rel="canonical" href="https://www.caraudioinc.com/contact" />
<link href="css/multiColumnTemplate.css" hreflang="en" rel="stylesheet" type="text/css">
<link rel="icon" href="images/favicon.png" type="image/png">
</head>

<body>
<div class="container">

  <header>
     <div class="primary_header">


    <nav class="secondary_header" id="menu">
      <ul>
     <li><a href="/">HOME</a></li>
        <li><a href="about">ABOUT</a></li>
          <li><a href="services">SERVICES</a></li>
        <li class="current"><a href="contact">CONTACT</a></li>
      </ul>
    </nav>

  </header>

 <div class="contactForm">
  <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"><br>

    <label for="name">Name&nbsp;&nbsp;</label><span class="error"><?php echo $errName; ?></span><br>
    <input type="text" id="name" name="name" value="<?php echo $name;?>"><br><br>



      <label for="email">Email&nbsp;&nbsp;</label><span class="error"><?php echo $errEmail; ?></span><br>
    <input type="email" id="email" name="email" value="<?php echo $email;?>"><br><br>



      <label for="pNum">Phone Number (optional)<br></label>
    <input type="tel" id="pNum" name="pNum" value="<?php echo $pNum;?>"><br><br>


      <label for="message">Message&nbsp;&nbsp;</label><span class="error"><?php echo $errMessage; ?></span><br>
    <textarea id="message" name="message" style="height:100px"><?php echo $message;?></textarea>

     <div class="g-recaptcha" data-sitekey="6LcR-DQUAAAAAB2cXajVPi7lpFEZ_z8vZrS97ZXo"></div><br>



    <input type="submit" value="Submit">
    </form>


  <footer class="secondary_header footer">
    <div class="copyright">&copy; Car Audio Inc 2020, All Rights Reserved.</div>

  </footer>

</div>
</body>
</html>
2 Answers

You could set the isset() function in your if() condiition as

if (isset($_POST)){...} 

By this it means that if something is posted or if $_POST os set to some value the codes moves forward.

But as your mentioned way earlier it was just checking that the form was posted or not and every time the form was being posted with no values at all and the code was moving further.

I finally figured it out.

Posting the answer for future n00bs like me, hope it helps someone:

<?php

$to = 'example@caraudioinc.com';
$subject = 'Car Audio Inc Form Message';

$errName = $errEmail = $errMessage = $errCaptcha = "";
$name = $email = $pNum = $message = "";



if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = test_input($_POST["name"]);
  $email = test_input($_POST["email"]);
  $pNum = test_input($_POST["pNum"]);
  $message = test_input($_POST["message"]);

}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if (empty($_POST["name"])) {
    $errName = "*Name field is required.";
    } else {
    $name = test_input($_POST["name"]);

        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $errName = "Invaild name."; 
        }
    }

    if (empty($_POST["email"])) {
    $errEmail = "*Email field is required.";
        } else {
    $email = test_input($_POST["email"]);

        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid E-mail."; 
        }
    }

    if (empty($_POST["message"])) {
    $errMessage = "*Message field is required.";
        } else {
    $message = test_input($_POST["message"]);
    }

    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
  {
        $secret = 'insert-your-private-key-here';
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);

  } else {
            $errCaptcha = '*reCAPTCHA failed.';
        }


$body = <<<EMAIL

Message from: $name

$message


Phone: $pNum
Email: $email

EMAIL;

$header = "From: $email";   

if (!$errName && !$errEmail && !$errMessage && $responseData->success) {
    if($_POST) {



        mail($to, $subject, $body, $header);

        header('Location: /sent');
    }

    }
}
?>

Related