google recaptcha implementation

    <!DOCTYPE HTML>
    <!– DO NOT FORGET TO CHANGE !YOUR_SECRET_KEY! to YOUR SECRET KEY FROM GOOGLE ADMIN CONSOLE – row 24 –>
    <html>
    <head>
    <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
    <title>reCAPTCHA v2 | response</title>
    </head>
    <body>
    <?php
    foreach ($_POST as $key => $value) {
    echo ‘key: ‘.$key.’ value: ‘.$value.'<br />’;
    }

    if(isset($_POST[‘g-recaptcha-response’])) {
    $captcha = $_POST[‘g-recaptcha-response’];
    }

    if(!$captcha || empty($captcha)) {
    echo ‘recaptcha verification failed’;
    exit;
    }

    $secretKey = ‘!YOUR_SECRET_KEY!’;
    $ip = $_SERVER[‘REMOTE_ADDR’];

    $url = ‘https://www.google.com/recaptcha/api/siteverify?secret=’.urlencode($secretKey).’&response=’.urlencode($captcha);
    $response = file_get_contents($url);
    $responseKeys = json_decode($response, true);

    if($responseKeys[“success”]) {
    echo ‘captcha verification succesful’;
    } else {
    echo ‘Hello, robot!’;
    }
    ?>
    </body>
    </html>

    Baby Asked on January 25, 2022 in HTML5 and CSS3.
    Add Comment
  • 1 Answer(s)

      <!DOCTYPE HTML>
      <html>
      <head>
      <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
      <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
      <title>reCAPTCHA v2 | response</title>
      </head>
      <body>
      <?php
      foreach ($_POST as $key => $value) {
      echo ‘key: ‘.$key.’ value: ‘.$value.'<br />’;
      }

      if(isset($_POST[‘g-recaptcha-response’])) {
      $captcha = $_POST[‘g-recaptcha-response’];
      }

      if(!$captcha || empty($captcha)) {
      echo ‘recaptcha verification failed’;
      exit;
      }

      $secretKey = ‘!YOUR_SECRET_KEY!’;
      $ip = $_SERVER[‘REMOTE_ADDR’];

      $url = ‘https://www.google.com/recaptcha/api/siteverify?secret=’.urlencode($secretKey).’&response=’.urlencode($captcha);
      $response = file_get_contents($url);
      $responseKeys = json_decode($response, true);

      if($responseKeys[“success”]) {
      echo ‘captcha verification successful’;
      } else {
      echo ‘Hello, robot!’;
      }
      ?>
      </body>
      </html>

      This code appears to be an HTML form with PHP code that implements Google reCAPTCHA v2 verification. reCAPTCHA is a service provided by Google that helps to prevent automated bots from submitting forms on websites. The purpose of this code is to validate whether the user filling out the form is a human or a robot.

      Here’s a breakdown of the code:

      DOCTYPE Declaration: This is a declaration specifying the type of HTML document. It’s not directly related to reCAPTCHA but indicates that the document is an HTML file.

      HTML Head:

        • <meta> tags are used to define metadata about the HTML document, such as character encoding and viewport settings.
        • The <title> tag sets the title of the web page.

      HTML Body:

        • This section contains the main content of the web page.

      PHP Code:

        • foreach ($_POST as $key => $value): This loop iterates through all the POST parameters sent to the server, which are likely the form fields submitted by the user. It then prints out the keys and values of these parameters.
        • The purpose of this loop seems to be for debugging or logging purposes, showing the values of all the form fields that were submitted.
      •  

      reCAPTCHA Verification:

        • if(isset($_POST['g-recaptcha-response'])): This checks if the ‘g-recaptcha-response’ field is present in the POST data. This field contains the response token generated by reCAPTCHA when the user completes the reCAPTCHA challenge.
        • The value of the ‘g-recaptcha-response’ field is assigned to the variable $captcha.

      Cecking Captcha and Verification:

        • if(!$captcha || empty($captcha)): This checks if the $captcha variable is empty or not set. If it’s empty, it indicates that the user didn’t complete the reCAPTCHA challenge, and the script displays an error message and exits.
        • The secret key for reCAPTCHA ($secretKey) is set to a placeholder value (‘!YOUR_SECRET_KEY!’). This should be replaced with your actual reCAPTCHA secret key obtained from the Google Admin Console.
        • The user’s IP address ($ip) is obtained using $_SERVER['REMOTE_ADDR'].

      Making the Verification Request:

        • The script constructs a URL for making a request to Google’s reCAPTCHA verification endpoint using the provided secret key and the user’s response token.
        • The file_get_contents($url) function is used to make an HTTP request to the verification endpoint and retrieve the response.

      Parsing and Responding to Verification:

        • The response from the reCAPTCHA verification endpoint is parsed using json_decode().
        • If the success field in the response is true, it means the reCAPTCHA challenge was successfully completed, and the script displays a success message.
        • If the success field is false, it means the challenge was failed, and the script displays a message indicating that a robot was detected.

      HTML End:
      The HTML and PHP code is closed with the </body> and </html> tags.

      To use this code, you need to replace '!YOUR_SECRET_KEY!' with your actual reCAPTCHA secret key obtained from the Google Admin Console. You also need to integrate this code with a form submission process on your website. The purpose of reCAPTCHA is to prevent automated bots from submitting forms, so when a user submits the form, their response token is validated with Google’s servers to confirm they are not a bot.

      Younger Kid Answered on August 11, 2023.
      Add Comment

      Your Answer

      By posting your answer, you agree to the privacy policy and terms of service.