PHP code example of tmi / anti-spam

1. Go to this page and download the library: Download tmi/anti-spam 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/ */

    

tmi / anti-spam example snippets


// config/bundles.php
return [
    // ...
    Tmi\AntiSpam\TmiAntiSpamBundle::class => ['all' => true],
];

public function configureOptions(OptionsResolver $resolver): void
{
    $resolver->setDefaults([
        'data_class' => MyRequest::class,
        'honeypot'   => true, // injects website / _loaded_at / _js_token (mapped:false)
    ]);
}

use Tmi\AntiSpam\HoneypotFields;
use Tmi\AntiSpam\Service\SpamGuard;

$reason = $spamGuard->detect(
    (string) $form->get(HoneypotFields::WEBSITE)->getData(),
    (string) $form->get(HoneypotFields::LOADED_AT)->getData(),
    (string) $form->get(HoneypotFields::JS_TOKEN)->getData(),
    MyType::JS_TOKEN,
);

if (null !== $reason) {
    // silently drop — no flash, no persistence
}

use Tmi\AntiSpam\Service\TurnstileVerifier;

final class AppTurnstileVerifier extends TurnstileVerifier
{
    public function __construct(HttpClientInterface $turnstileClient, string $secret, LoggerInterface $logger)
    {
        parent::__construct($turnstileClient, $secret, $logger, ['example.com', 'www.example.com']);
    }
}

// in the controller, BEFORE the honeypot check:
$reason = $verifier->verify(
    $request->request->getString('cf-turnstile-response'),
    $request->getClientIp() ?? '',
    'newsletter', // the action your widget declared
);
// null = ok; otherwise 'missing-token'|'invalid-token'|'timeout-or-duplicate'|'hostname-mismatch'|'action-mismatch'|'network-error'