PHP code example of altcha-org / altcha

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

    

altcha-org / altcha example snippets




ltchaOrg\Altcha\ChallengeOptions;
use AltchaOrg\Altcha\Altcha;

$altcha = new Altcha('secret hmac key');

// Create a new challenge
$options = new ChallengeOptions(
    maxNumber: 50000, // the maximum random number
    expires: (new \DateTimeImmutable())->add(new \DateInterval('PT10S')),
);

$challenge = $altcha->createChallenge($options);
echo "Challenge created: " . json_encode($challenge) . "\n";

// Example payload to verify
$payload = [
    'algorithm' => $challenge->algorithm,
    'challenge' => $challenge->challenge,
    'number'    => 12345, // Example number
    'salt'      => $challenge->salt,
    'signature' => $challenge->signature,
];

// Verify the solution
$ok = $altcha->verifySolution($payload, true);

if ($ok) {
    echo "Solution verified!\n";
} else {
    echo "Invalid solution.\n";
}

$options = new ChallengeOptions(
    algorithm: Algorithm::SHA256,
    maxNumber: BaseChallengeOptions::DEFAULT_MAX_NUMBER,
    expires: (new \DateTimeImmutable())->add(new \DateInterval('PT10S')),
    params: ['query_param' => '123'],
    saltLength: 12
);
sh
vendor/bin/phpunit --bootstrap src/Altcha.php tests/AltchaTest.php