Google reCaptcha v3 on NON-WP sites

  1. Make sure you are logged in to your gMail
  2. Register your website & get your key
    GoTo: http://www.google.com/recaptcha/admin
    Click the + in top right to create new
    Type in the Website Name
    Choose v3
    Type in the Domain
    You will be admin
    Agree to Terms
    Submit
  3. You now have a site key & secret key
  4. In form html page header:
    <script src=”https://www.google.com/recaptcha/api.js?render= put your site key here “></script><script>    grecaptcha.ready(function () {        grecaptcha.execute(‘ put your site key here ‘, { action: ‘contact’ }).then(function (token) {            var recaptchaResponse = document.getElementById(‘recaptchaResponse’);            recaptchaResponse.value = token;        });    });</script>
  5. In form html page above </form>
    <input type=”hidden” name=”recaptcha_response” id=”recaptchaResponse”>
  6. In your php processing script:

<?php // Check if form was submitted:
if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’ && isset($_POST[‘recaptcha_response’])) {

// Build POST request:
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = 'Your secret key here';
$recaptcha_response = $_POST['recaptcha_response'];

// Make and decode POST request:
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
$recaptcha = json_decode($recaptcha);

// Take action based on the score returned:
if ($recaptcha->score >= 0.5) {
  // multiple recipients
  $to  = 'mary@dossdesigns.com' . ', '; // note the comma
  // if you want to cc others $to .= 'email@yahoo.com';

  // subject
  $subject = 'Website Inquiry Form';

  // get variables from form
  $email = $_REQUEST ['email'];
  $name = $_REQUEST ['name'];
  $street = $_REQUEST ['street'];
  $city = $_REQUEST ['city'];
  $state = $_REQUEST ['state'];
  $zip = $_REQUEST ['zip'];
  $phone = $_REQUEST ['phone'];
  $comments = $_REQUEST ['comments'];



  // message
  $message = '
  Name:    '.$name.'

  Email    '.$email.'

  Address:    '.$street.', '.$city.', '.$state.'  '.$zip.'

  Phone    '.$phone.'

  Comments    '.$comments.'

  ';

//Put your html for your thank you below here


Most From: https://codeforgeek.com/google-recaptcha-v3-tutorial/

Leave a Reply