21 July 2021

How to add google reCAPTCHA in form in PHP?

Add google reCAPTCHA in form in PHP





<html>
<head>
  <script src='https://www.google.com/recaptcha/api.js' async defer></script>
</head>
<body>
<?php 
if(isset($_POST['submit'])){
$captcha;

        if(isset($_POST['g-recaptcha-response'])){
          $captcha=$_POST['g-recaptcha-response'];
        }
        if(!$captcha){
          echo 'Please check the the captcha form.';
          exit;
        }
        $secretKey = "<!==== Secret key ====!>";
        $ip = $_SERVER['REMOTE_ADDR'];
        // post request to server
        $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) .  '&response=' . urlencode($captcha);
        $response = file_get_contents($url);
        $responseKeys = json_decode($response,true);
        // should return JSON with success as true
        if($responseKeys["success"]) {
                echo 'Thanks for posting';
echo $_POST['name'];
        } else {
                echo 'You are spammer ! Get the @$%K out';
        }

}
?>
<form action="" method="post">
<input type="text" name="name" placeholder="name" value="Hello World">
<div class="g-recaptcha" data-sitekey="<!==== Site key ====!>"></div>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

No comments:

Post a Comment

How to create youtube videos slider with play and pause option in wordpress?

Create youtube videos slider with play and pause option in wordpress youtube videos slider Use this shortcode:- [punjab_today] function my_...