A simple implementation of hCaptcha on PHP >= 8.1.
- Easy to integrate hCaptcha with customizable themes and sizes.
- Supports verification of hCaptcha responses.
- Utilizes user IP address for verification.
- Example implementation provided.
-
Clone the repository:
git clone https://github.com/ycomiti/Simple-PHP-HCaptcha.git
-
Navigate to the project directory:
cd Simple-PHP-HCaptcha
-
Make sure to set up your configuration file (
src/config.php
) with your hCaptcha public and private keys.
You will need to replace the placeholders in the example code with your actual hCaptcha keys:
$hCaptcha = new HCaptcha(
"your_public_key", // Replace with your public key
"your_private_key", // Replace with your private key
HCaptchaTheme::DARK // Optional: Choose the theme
);
Use the following HTML structure to display the hCaptcha widget:
<form method="POST">
<?= $hCaptcha->display() ?>
<button type="submit">Submit</button>
</form>
To verify the user's response, you can handle the POST request as follows:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['h-captcha-response'])) {
$hCaptcha->verify($_POST['h-captcha-response']);
$human = (boolean)$hCaptcha->isHuman();
if ($human) {
// User is verified as human
echo "Verification successful!";
} else {
// Verification failed
echo "Verification failed!";
}
}
}
This class handles the hCaptcha integration. It requires the following parameters upon initialization:
- publicKey: Your hCaptcha public key.
- secretKey: Your hCaptcha secret key.
- theme: Optional theme for the captcha (defaults to LIGHT).
- size: Optional size for the captcha (defaults to NORMAL).
- url: Optional verification URL (defaults to
https://hcaptcha.com/siteverify
). - scriptUrl: Optional script URL (defaults to
https://hcaptcha.com/1/api.js
).
A utility class to retrieve the user's IP address.
- HCaptchaTheme: Defines the theme options (LIGHT, DARK).
- HCaptchaSize: Defines the size options (NORMAL, COMPACT).
A complete example is provided in the index.php
file, demonstrating how to set up and use the HCaptcha
class in a simple HTML form.
This project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.
- hCaptcha for providing the captcha service.
You can find the project repository here.