PHP code example of chillerlan / 2fa-qrcode-bundle

1. Go to this page and download the library: Download chillerlan/2fa-qrcode-bundle 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/ */

    

chillerlan / 2fa-qrcode-bundle example snippets


use chillerlan\TwoFactorQRCode\TwoFactorQRCode;

$options = [
	'secret_length' => 128,
	'adjacent'      => 2,
];

$twoFactorQRCode = new TwoFactorQRCode($options);

$newSecret = $twoFactorQRCode->createSecret();
$qrcode    = $twoFactorQRCode->getQRCode('label', 'issuer');
$backup    = $twoFactorQRCode->createBackupCode($counterValue);

if($twoFactorQRCode->verifyOTP($newOTP)){
	echo 'yay! do not forget to save your backup code!';
}

$twoFactorQRCode->setSecret($userSecret);

if($twoFactorQRCode->verifyOTP($OTP)){
	// do stuff...

	// redirect
	header('Location: ...');
}

$twoFactorQRCode->setSecret($userSecret);

if($twoFactorQRCode->verifyBackupCode($backupOTP, $counterValue)){
	$counterValue++;

	$newBackup = $twoFactorQRCode->createBackupCode($counterValue);

	// redirect
}

$twoFactorQRCode->setSecret($userSecret);

$currentBackup = $twoFactorQRCode->createBackupCode($currentCounterValue);
$qrcode        = $twoFactorQRCode->getQRCode('label', 'issuer');

$tempSecret       = $twoFactorQRCode->createSecret();
$tempCounter      = random_int(1, 999999);
$verificationCode = $twoFactorQRCode->createBackupCode($counterValue);

$twoFactorQRCode->setSecret($tempSecret);

if($twoFactorQRCode->verifyBackupCode($verificationCode, $tempCounter)){
	// success!
}