PHP code example of pamellayamada / otphp

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

    

pamellayamada / otphp example snippets


use PamellaYamada\OTPHP\OTPHP;

// Generate a secure Base32 secret key
$secret = OTPHP::createSecret(32); 


use PamellaYamada\OTPHP\OTPHP;
use PamellaYamada\OTPHP\Enums\OTPProvider;

// Generate the current 6-digit time-based code (Default: Google Authenticator)
$code = OTPHP::generate($secret);

// Or specify a target provider (e.g., Aegis with 8 digits)
$aegisCode = OTPHP::generate($secret, OTPProvider::AEGIS);


use PamellaYamada\OTPHP\OTPHP;

$userCode = '123456';

// Verify the code considering time drift (window parameter)
$isValid = OTPHP::verify($userCode,$secret, window: 1);

if ($isValid) {
    // Authentication successful
}


use PamellaYamada\OTPHP\OTPHP;

// Returns a clean <svg> string ready to embed into HTML, Blade, or PHP views
$svgXml = OTPHP::renderQrCodeSvg(
    secret: $secret,
    accountName: '[email protected]',
    issuer: 'MyApp',
    sizePixels: 200
);

echo $svgXml;


use PamellaYamada\OTPHP\OTPHP;
use PamellaYamada\OTPHP\Enums\OTPLanguage;

// Set global locale for exception messages and output
OTPHP::setLocale(OTPLanguage::EN_US);