PHP code example of olakunlevpn / phpcapcha

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

    

olakunlevpn / phpcapcha example snippets


use Olakunlevpn\Captcha\Captcha;

session_start();

// Initialize
$captcha = new Captcha();

// Generate and Output an Image (200x70)
$captcha->create()->output();

// Store the code in session for validation later
$_SESSION['captcha_code'] = $captcha->getCode();

use Olakunlevpn\Captcha\Captcha;

$captcha = new Captcha();

$captcha->setFont(__DIR__ . '/fonts/Monaco.ttf') // Or pass a directory for random font
        ->setTextColor(40, 40, 40)               // Dark Grey Text
        ->setBackgroundColor(240, 240, 240)      // Light Grey Background
        ->setLength(5)                           // 5 Characters
        ->setType('alpha')                       // Alphabetic only (no numbers)
        ->create(250, 80);                       // Create with specific dimensions

$captcha->output();

$captcha = new Captcha();

// Set type to 'math'
$captcha->setType('math');

$image = $captcha->create();

// The code will be the result (e.g., if image shows "5 + 3", code is "8")
$result = $captcha->getCode();

$captcha->output();

$captcha = new Captcha();

// Enable/Disable specific elements
$captcha->setNoise(true, 100)      // Enabled, 100 dots
        ->setLines(true, 5)        // Enabled, 5 random lines
        ->setDistortion(true, 5, 40) // Enabled, Amplitude 5, Period 40
        ->create();

$captcha->setNoise(false)
        ->setLines(false)
        ->setDistortion(false)
        ->create();

$captcha->create();
$captcha->save(__DIR__ . '/captchas/image_123.png');

$captcha->create();
$base64 = $captcha->getBase64();

echo '<img src="' . $base64 . '" />';

session_start();

$input = $_POST['captcha']; // User input
$stored = $_SESSION['captcha_code']; // Stored from generation

// Use case-insensitive comparison for text
if (strtolower($input) === strtolower($stored)) {
    echo "Verification Successful!";
} else {
    echo "Incorrect Code.";
}