PHP code example of fernet / captcha

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

    

fernet / captcha example snippets



namespace App\Component;

use CaptchaFernet\Captcha;
use Symfony\Component\HttpFoundation\Request;

class TodoForm 
{
    private Captcha $captcha;
    private string $message = 'Press the submit button';

    public function __construct(Captcha $captcha)
    {
        $this->captcha = $captcha;
    }
    
    public function handleSubmit(Request $request)
    {
        if (!$this->captcha->verify($request)) {
            $this->message = 'Captcha ok!';
        } else {
            $this->message = 'Error Captcha';
        }
    }

    public function __toString(): string
    {
        return <<<EOH
            <h1>{$this->message}</h1>
            <form @onSubmit="handleSubmit">
                <Captcha />
                <button>Submit</button>
            </form>
EOH;
    }
}


namespace App\Component;

class App 
{
    public $preventWrapper = true;

    public function __toString(): string
    {
        return <<<EOH
            <html>
            <body>
                <TodoForm />
                <CaptchaScript />
            </body>
            </html>
EOH;
    }
}
ini
CAPTCHA_SITE="paste the site code"
CAPTCHA_SECRET="paste de secret code"