Field | Value | Explaination |
---|---|---|
Label | localhost | We put localhost since we're using this server for development |
Choose the type of reCAPTCHA | reCAPTCHA v2 | This is what we are using |
Domains | localhost 127.0.0.1 | This is so we can use recaptcha in any localhost development |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>How to Add Google ReCaptcha</title> <link rel="stylesheet" type="text/css" href="bootstrap4/css/bootstrap.min.css"> <script src='https://www.google.com/recaptcha/api.js'></script> <style type="text/css"> .mt20{ margin-top:20px; } .mt10{ margin-top:10px; } </style> </head> <body> <div class="container"> <h1 class="text-center mt20">How to Add Google reCaptcha</h1> <hr> <div class="row justify-content-md-center"> <div class="col-sm-5"> <?php echo " <div class='alert alert-danger text-center'> ".$_SESSION['error']." </div> "; } echo " <div class='alert alert-success text-center'> ".$_SESSION['success']." </div> "; } ?> <form method="POST" action="submit.php"> <div class="g-recaptcha" data-sitekey="6LevO1IUAAAAAFX5PpmtEoCxwae-I8cCQrbhTfM6"></div> <button type="submit" class="btn btn-primary mt10" name="submit">Submit</button> </form> </div> </div> </div> </body> </html>
<?php require('recaptcha/src/autoload.php'); $recaptcha = new \ReCaptcha\ReCaptcha('6LevO1IUAAAAAFCCiOHERRXjh3VrHa5oywciMKcw', new \ReCaptcha\RequestMethod\SocketPost()); $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']); if (!$resp->isSuccess()){ $_SESSION['error'] = 'Please answer recaptcha correctly'; } else{ $_SESSION['success'] = 'Captcha Verified User'; } } else{ $_SESSION['error'] = 'Please verify that you are human'; } } ?>