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

 

Google reCaptcha v2 Doesn’t Work using PHP on my Website

My website is a WordPress website. I wanted to create a form with a reCaptcha protection using Google reCaptcha v2 (tick box). Surprisingly it doesn’t work even though it works on my other website using same PHP code.

After Ticking the reCaptcha

google recaptcha v2 tick box
google recaptcha v2 tick box

It doesn’t work if use file_get_contents

Google reCaptcha keeps sending me error.

It works if using curl

 

 

PHP is not running after upgrade MacOS Big Sur on Apple Macbook Pro M1

Another problem I always face after upgrading MacOS where PHP is not running anymore. I have to google to find solution. Now I decided to write step by step tutorial so easy for my future reference and hopefully can help others who face the same thing.

1) My phpMyAdmin instead of showing login page, it displays its PHP code.

Macos Bigsur upgrade php not running
Macos Bigsur upgrade php not running

2) Uncomment PHP Module

Open httpd.conf

Find php by typing

Remove # to uncomment the PHP module.

remove # from load module php
remove # from load module php

Save the changes by typing

3) Restart Apache

4) Refresh the PHP page

macos big sur php running again after change httpd conf
macos big sur php running again after change httpd conf

PHP Serialize String and JSON Object

I’m not really understand difference between PHP serialize string and JSON object.

I notice PHP when stored data inside database is in PHP serialize string format. Serialize string is only available in PHP, hence it is not interoperable with NodeJS.

Hence, I did simple comparison for better understanding in order to make my NodeJS can read and write PHP data and vice versa.

PHP Array Conversion to PHP Serialize String and JSON Object

PHP Object Conversion to PHP Serialize String and JSON Object

PHP Array of Object Conversion to PHP Serialize String and JSON Object