Pages

Sunday, 25 August 2019

Google Recaptcha

Google Recaptcha

What is reCAPTCHA?




reCAPTCHA is a free service that protects your website from spam and abuse. reCAPTCHA uses an advanced risk analysis engine and adaptive challenges to keep automated software from engaging in abusive activities on your site. It does this while letting your valid users pass through with ease.

This tutorial will help to add recaptcha with a PHP. Follow the steps as below.
  • Login into gmail
  • search google recaptcha open recaptcha website 
  • Click admin console
  • Click + icon at the top right corner 
  • Enter label name for identification function
  • Select recpatcha tyeps v2 or v3
  • Enter your domain and click + icon
  • Uncheck Accept the reCAPTCHA Terms of Service otherwise recaptcha would not be run on localhost
  • Get Keys by click setting > reCaptcha Key




Sample Code :


<?php
     require_once "recaptchalib.php";
     $msg="";
     if(isset($_POST['submit']))
     {
          $secret = "6LezwLQUAAAAAJr_FwpPUsB-MMiNIVN1NQbTJkDl";
          $response = null;
          $reCaptcha = new ReCaptcha($secret);
          if ($_POST["g-recaptcha-response"]) {
              $response = $reCaptcha->verifyResponse(
                   $_SERVER["REMOTE_ADDR"],
                   $_POST["g-recaptcha-response"]
              );
          }
          if ($response != null && $response->success)
          {
              $msg="validated put your code here";
          }
          else
              $msg="<span style='color:red'>Invalid Captcha try again</span>";
     }
?>
<html>
     <head>
          <title>Google Recaptcha : Girfa Student Help</title>
      <script src="https://www.google.com/recaptcha/api.js" async defer></script>
     </head>
<body>
     <h1 style="text-align:center">Google Recaptcha</h1><hr />
     <form method="post">
          User Name : <input type="text" name="txtUser" placeholder="Enter User Name " required /><br /><br />
          Password : <input type="password" name="txtPass" Placeholder="Enter Password" required /><br /><br />     
          <div class="g-recaptcha" data-sitekey="6LezwLQUAAAAAAf6IxjQcXc0qJoqYcz9-MJHO2Up"></div>                            
          <input type="submit" value="Login" name="submit" /><br /><br />
          <?php echo $msg;?>
     </form>
</body>  
</html>






No comments:

Post a Comment