PHP code example of myra-security-gmbh / eu-captcha
1. Go to this page and download the library: Download myra-security-gmbh/eu-captcha library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
myra-security-gmbh / eu-captcha example snippets
use Myrasec\EuCaptcha;
$captcha = new EuCaptcha(
sitekey: EUCAPTCHA_SITE_KEY,
secret: EUCAPTCHA_SECRET_KEY,
);
$result = $captcha->validate();
if (!$result->success()) {
// Reject the form submission
}
$result = $captcha->validate();
if (!$result->successNetwork()) {
// Could not reach the API — consider logging or alerting
}
if (!$result->successToken()) {
// Token was rejected — the submission is likely automated
}
$captcha = new EuCaptcha(sitekey: EUCAPTCHA_SITE_KEY, secret: EUCAPTCHA_SECRET_KEY);
if (!$captcha->verifyCredentials()) {
// Credentials are invalid or the API is unreachable — log and alert
}
namespace App\Controller;
use Myrasec\EuCaptchaInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class ContactController
{
public function __construct(private EuCaptchaInterface $captcha) {}
#[Route('/contact', methods: ['POST'])]
public function submit(Request $request): Response
{
$result = $this->captcha->validate(
$request->request->getString('eu-captcha-response'),
$request->getClientIp() ?? '',
);
if (!$result->success()) {
return new Response('CAPTCHA verification failed', Response::HTTP_BAD_REQUEST);
}
// process the form...
return new Response('OK');
}
}
namespace App\Http\Controllers;
use Myrasec\EuCaptcha;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
class ContactController extends Controller
{
public function submit(Request $request): RedirectResponse
{
$captcha = new EuCaptcha(
sitekey: config('services.eucaptcha.sitekey'),
secret: config('services.eucaptcha.secret'),
);
$result = $captcha->validate(
$request->input('eu-captcha-response'),
$request->ip(),
);
if (!$result->success()) {
return back()->withErrors(['captcha' => 'CAPTCHA verification failed.']);
}
// process the form...
return redirect()->route('contact.success');
}
}
namespace App\Http\Requests;
use Myrasec\EuCaptcha;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
class ContactRequest extends FormRequest
{
public function rules(): array
{
return [
'name' => ['cha(
sitekey: config('services.eucaptcha.sitekey'),
secret: config('services.eucaptcha.secret'),
);
$result = $captcha->validate(
$this->input('eu-captcha-response'),
$this->ip(),
);
if (!$result->success()) {
$validator->errors()->add('captcha', 'CAPTCHA verification failed.');
}
});
}
}
public function submit(ContactRequest $request): RedirectResponse
{
// validation and CAPTCHA check already passed
// process the form...
return redirect()->route('contact.success');
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.