PHP code example of visavi / captcha

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

    

visavi / captcha example snippets


use Visavi\Captcha\CaptchaBuilder;

header('Content-Type: image/gif');

$captcha = new CaptchaBuilder();
$_SESSION['captcha'] = $captcha->getPhrase();

echo $captcha->render();

$captcha = new CaptchaBuilder();
$_SESSION['captcha'] = $captcha->getPhrase();

echo '<img src="' . $captcha->inline() . '" alt="captcha">';

$phrase = $_SESSION['captcha'] ?? '';
unset($_SESSION['captcha']);

if ($phrase === '' || ! hash_equals(strtolower($phrase), strtolower(trim($_POST['captcha'] ?? '')))) {
    // wrong answer
}

use Visavi\Captcha\CaptchaBuilder;
use Visavi\Captcha\PhraseBuilder;

$phrase = (new PhraseBuilder())->getPhrase(5, '1234567890');

$captcha = (new CaptchaBuilder($phrase))
    ->setWidth(150)
    ->setHeight(50)
    ->setTextColor(0, 0, 0)
    ->setBackgroundColor(255, 255, 255)
    ->setFont('/path-to-font.ttf')
    ->setWindowWidth(60)
    ->setPixelPerFrame(5)
    ->setDelayBetweenFrames(10);

$phrase = new PhraseBuilder();

$phrase->getPhrase();                       // 6 characters, default charset
$phrase->getPhrase(5, '1234567890');        // digits only
$phrase->getPhrase(4, 'абвгдежзик');        // any UTF-8 charset