hCaptcha Demo using PHP

hCaptcha is alternative to popular Google reCaptcha. It is used to avoid any form from being spammed by bots.

Why I use hCaptcha?

According to hCaptcha website owner can earn income by using their captcha technology. For the moment, I haven’t earned anything yet from hCaptcha.

earn income using hCaptcha
earn income using hCaptcha

Demo hCaptcha Using PHP

Demo hCaptcha

For this demo, you just submit the form by clicking “Submit form”. If you do the the hCaptcha correctly you will get hCaptcha success if not hCaptcha fail.

hcaptcha demo submit success
hcaptcha demo submit success
hcaptcha submit fail
hcaptcha submit fail

How to Use hCaptcha Using PHP?

There are 3 Important steps:

  1. Add the hCaptcha widget to your webpage
  2. Verify the user response on server side
  3. Check the hCaptcha is ticked or no

Add the hCaptcha widget to your webpage

Add the script at HTML head

Add the hCaptcha widget to your form

Verify the user response on server side

According to hCaptcha documentation, to use POST method to query their endpoint.

Example of Success Response

If you print_r($json_data) will get the response below:

You can do your programming logic based on the success status.

Check the hCaptcha is ticked or no

There 2 methods to do this

  1. Check the post value of ‘h-captcha-response’
  2. Use data-callback

Method #1: Check the post value of ‘h-captcha-response’

The problem with the form as it is, if a user doesn’t do the hCaptcha test, the user still can submit the form. So we need to add javascript checking on the client side to check whether the user has done the captcha test or no.

If you have done the captcha test, hCaptcha will send a string of response in the $_POST[‘h-captcha-response’]. If not this string will be empty.

hcaptcha not done alert
hcaptcha not done alert

 

The Full Code for Method #1

 

Method #2 – Use data-callback

Demo #2 Use Data Callback

hCaptcha has a call back option for when the checkbox is checked.

Disable the submit button

Then create a callback function:

The Full Code for Method #2