PHP code example of atomjoy / google-authenticator
1. Go to this page and download the library: Download atomjoy/google-authenticator 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/ */
atomjoy / google-authenticator example snippets
use Atomjoy\GoogleAuthenticator\GoogleAuthenticator;
use Illuminate\Support\Facades\Route;
// Get GoogleAuthenticator code
Route::get('/test/2fa/google/{secret}', function ($secret) {
// Authenticator
$ga = new GoogleAuthenticator();
// Create secret for user (first time)
if ($secret == 'create') {
// Create secret enable 2fa (save secret in database)
$secret = $ga->createSecret();
// QR-Code image
$url = $ga->getQRCodeUrl('Appname:[email protected]', $secret);
// Show user qr-code
echo "<h1>New secret is: " . $secret . "</h1>";
echo '<h2>Google Charts URL for the QR-Code:</h2>';
echo '<p><img src="' . $url . '"> </p>';
} else {
// Generate code from user secret like in GoogleAuthenticator on Android but from php script
// Past this code in github 2fa confirm code form
$code = $ga->getCode($secret);
echo "<p>Checking code <b>" . $code . "</b> and Secret <b>" . $secret . "</b> use this code in 2fa on github, facebook, ...</p>";
// Confirm code (allow 2*30sec clock tolerance)
if ($ga->verifyCode($secret, $code, 2)) {
echo '<h3 style="color: #5c5">OK</h3>';
} else {
echo '<h3 style="color: #f23">FAILED</h3>';
}
}
});