loading

How to Integrate CAPTCHA Checkbox with hCaptcha using PHP

How to Integrate CAPTCHA Checkbox with hCaptcha using PHP

How to Integrate CAPTCHA Checkbox with hCaptcha using PHP

0 Sales

Free

CAPTCHA is a procedure that determines whether or not a user is human. In most cases, the CAPTCHA is employed in web forms to prevent spam input. Without a CAPTCHA, bots may send spam information via automated form submission, filling your site with nothing but data. Including CAPTCHA on a website is usually a smart idea to avoid spam in online forms.

 

Captchas of many forms, such as questions, numerical calculation, checkboxes, and so on, can be utilised for the verification process. Before submitting the form, the user must answer the CAPTCHA and demonstrate that they are not a bot. Using PHP, you can simply implement CAPTCHA functionality into a web form. Checkbox Captcha is the greatest solution if you want to give a user-friendly method.

 

You may utilise a third-party API to simplify and secure the CAPTCHA integration process. Using PHP, the hCaptcha provides a simple and straightforward solution to add CAPTCHA to a webform. The greatest alternative to Google reCAPTCHA Checkbox is hCaptcha. In this article, we'll teach you how to use PHP to incorporate CAPTCHA functionality with hCaptcha.

 

Generate hCaptcha API Keys

To utilise the hCaptcha API, you must have the Site and Secret keys. Before you can add the CAPTCHA box to your website, you must first register it and obtain the API credentials.

- Visit the hCaptcha website and create an account.

- Sign in to your account and select the Add New Site option.

     - Name – This will aid in the identification of your listed site in the future.

     - Hostnames – Enter the domain name for your website.

 

- To register your site, click Save.

- The newly added site will appear underneath the Sites tab. By selecting the Settings option, you will be able to view the Sitekey.

- The Secret key may be found under the Settings tab.

Obtain the Site Key and Secret Key to be used in the PHP CAPTCHA integration code later.

 

Add hCaptcha Widget to HTML Form

Include the hCaptcha API JavaScript library first.

<script src="https://hcaptcha.com/1/api.js" async defer></script>


Insert the h-captcha tag element into the HTML form where you want the hCaptcha checkbox widget to appear.

- The h-captcha The DIV element has the h-captcha class and the data-sitekey properties.

- The data-sitekey element will include the hCaptcha API's Site Key.

<!-- Form fields -->
<form action="" method="post">
    <div class="input-group">
        <input type="text" name="name" value="" placeholder="Your name" required="" />
    </div>
    <div class="input-group">	
        <input type="email" name="email" value="" placeholder="Your email" required="" />
    </div>
    <div class="input-group">
        <textarea name="message" placeholder="Type message..."></textarea>
    </div>
		
    <!-- Add hCaptcha CAPTCHA box -->
    <div class="h-captcha" data-sitekey="Your_hCAPTCHA_Site_Key"></div>
	
    <!-- Submit button -->
    <input type="submit" name="submit" value="SUBMIT">
</form>

 

Verify hCaptcha Response (Server-side Validation)

 

After submitting the form, the input data will be sent to the server-side script, which will validate the user's answer and execute the form submission request.

- Validate form fields to ensure that the user has filled out all needed input areas.

- To determine if the user has selected the CAPTCHA checkbox, use the h-captcha-response POST argument.

- Use hCaptcha and PHP to validate the CAPTCHA challenge.

     - Create a PHP cURL call and POST the needed parameters to the hCaptcha API.

          - secret – Secret Key

          - The recipient's reply as delivered by $_POST['h-captcha-response'].

          - remoteip – The IP address of the user.

- Check for status by parsing JSON from the hCaptcha answer.

- if the hCaptcha reply is legitimate and successful.

     - The form is submitted

     - The user is presented the status message.

 

<?php 
 
// hCAPTCHA API key configuration 
$secretKey 'Insert_hCaptcha_Secret_Key'; 
 
// If the form is submitted 
$statusMsg ''; 
if(isset($_POST['submit'])){ 
     
    // Validate form fields 
    if(!empty($_POST['name']) && !empty($_POST['email'])){ 
         
        // Validate hCAPTCHA checkbox 
        if(!empty($_POST['h-captcha-response'])){ 
            // Verify API URL 
            $verifyURL 'https://hcaptcha.com/siteverify'; 
             
            // Retrieve token from post data with key 'h-captcha-response' 
            $token $_POST['h-captcha-response']; 
             
            // Build payload with secret key and token 
            $data = array( 
                'secret' => $secretKey, 
                'response' => $token, 
                'remoteip' => $_SERVER['REMOTE_ADDR'] 
            ); 
             
            // Initialize cURL request 
            // Make POST request with data payload to hCaptcha API endpoint 
            $curlConfig = array( 
                CURLOPT_URL => $verifyURL, 
                CURLOPT_POST => true, 
                CURLOPT_RETURNTRANSFER => true, 
                CURLOPT_POSTFIELDS => $data 
            ); 
            $ch curl_init(); 
            curl_setopt_array($ch$curlConfig); 
            $response curl_exec($ch); 
            curl_close($ch); 
             
            // Parse JSON from response. Check for success or error codes 
            $responseData json_decode($response); 
             
            // If reCAPTCHA response is valid 
            if($responseData->success){ 
                // Posted form data 
                $name = !empty($_POST['name'])?$_POST['name']:''; 
                $email = !empty($_POST['email'])?$_POST['email']:''; 
                $message = !empty($_POST['message'])?$_POST['message']:''; 
                 
                // Code to process the form data goes here... 
                 
                 
                $statusMsg 'Your contact request has submitted successfully.'; 
            }else{ 
                $statusMsg 'Robot verification failed, please try again.'; 
            } 
        }else{ 
            $statusMsg 'Please check on the CAPTCHA box.'; 
        } 
    }else{ 
        $statusMsg 'Please fill all the mandatory fields.'; 
    } 
} 
 
echo $statusMsg; 
 
?>

 

Conclusion

 

CAPTCHA functionality may be added to a website using both a third-party API and a proprietary PHP library. The hCaptcha is the simplest way to add a CAPTCHA challenge to a web form without using a library. It is the most equivalent to Google reCAPTCHA and the finest CAPTCHA checkbox option. Using PHP, you may implement a contact form with CAPTCHA and email capabilities on your website by following our sample code.

LICENSE OF USE

You can use it for personal or commercial projects. You can't resell it partially or in this form.

PRODUCT INFO

Create Date : Jan 22, 2022

Updated Date : Jan 23, 2022

Ratings

Comments : 0

Downloads : 0