Easy integration of hCaptcha, a service that
- Keeps bots out;
- Earns you money;
- Is privacy conscious.
-
Sign up at hCaptcha.
-
Fetch your public key and site key from the settings tab.
-
Get this package
composer require neoan3-apps/hcaptcha
-
Set up your front end as:
<head>
<script src="https://hcaptcha.com/1/api.js" async defer></script>
...
</head>
<body>
<form action="endpoint.php" method="post">
...
<div class="h-captcha" data-sitekey="your-sitekey"></div>
<input type="submit" value="send">
</form>
</body>
- Now in your PHP back end:
\Neoan3\Apps\Hcapture::setEnvironment([
'siteKey' => 'your-sitekey',
'secret' => 'your-secret',
'apiKey' => 'your-api-key'
]);
if(isset($_POST['my-form']) && \Neoan3\Apps\Hcapture::isHuman())
{
... do stuff
}
Here are some methods for you to use and setup hCaptcha in your project.
For your global environment setup, please use the following methods:
// Set your own secret key
setSecret(string $value);
// API Key of hCaptcha linked to your account
setApiKey(string $value);
// Secret site key linked to your site
// See: https://docs.hcaptcha.com/api#addnewsitekey
setSiteKey(string $value);
// With an array of environment variables provided, set all of them
// This is a 'shortcut' for all three previous methods
setEnvironment(array $environmentVariables);
To retrieve hCaptcha information, here are the methods you will need:
// Check if the hCaptcha verification was successful
isHuman();
// Retrieve all statitics of the site corresponding to the
// provided variables (an error value is returned in case of
// invalid credentials)
stats();
You can have more details about advanced setup on the configuration page of the documentation.