Padlock Icon


A simple, procedural PHP script to create Google Authenticator secrets and corresponding QR codes,
then verify the entered response over a given time variance.

Step 1.

Use Authenticatron_New to create a new secret for a member, and fetch a secure image for scanning.




$Member_Name is a string containing your members username or nice-name, perferably something unique and quickly identifiable.


Outputs an array, where Secret is the Secret for the member, URL is an OTPAuth URL, and QR is the Data64 URI for the QR code.

array(3) {
  string(16) "RCEIN5BHHFFJGZJG"
  string(83) "otpauth://totp/Example Site: John Smith?secret=RCEIN5BHHFFJGZJG&issuer=Example+Site"
  string(618) "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJQAAACUAQMAAABP8pKXAAAABlBMVEUAAAD///+l2Z/dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABX0lEQVRIia2W6w2DQAyDs0H239IbuLHDo7/PIFWFD4kcsZ2jqqqr0UCzwLqOY9bkMILdGDaXIatun0+VKt3PGdF69qy5P2Fbp9WHnE0PMP+gWvr25ZRJkr/j0e2UcY7pJlVj1q2riI3SLgIpL+lVNGHyDyz2eEgdRcRwO9xiUU6P2Px0Ys23AyG7Xel+Sp+M0UGBhOGWQ8RmLLRv1GXOqoyBFmlgr/aI2PpaZeZWPYE5ZuvrtaPGjmokTDkuz9dygq58H7PRZFoAaa6F6zxkNrkVvwZZyCy3e6o36MqYU608e/RoeFfEHDwZfGehR0XC2PYj2zLtTEuYrDgCyZDqrz0eMXiNfnn5hxvCY+ahSove1v8etKeMtTnm24SE0cLwvm0XJGxfmV773zZ6zLTH++Ea3JvtjOn7w1teOzX9AVMBNdYfhmpGyJQcW2kDnTFa7B0W8BYVMWuE3V52uRH7AQcqoT2+LgjMAAAAAElFTkSuQmCC"


You'll want to store ['Secret'] with the member, but make sure you get them to confirm a code before enforcing it, or it might not have worked and they would be locked out of their account. Make sure that this is as protected as a password hash.

['QR'] is the Data64 URI for the QR code. You can simply echo it into an img element like this:

<img src="<?php echo $SecondAuth['QR']; ?>" alt="Second Factor Authentication Code">


Google Camera Icon

Try scanning this into an app like Google Authenticator. You should see a code and a countdown clock until it changes.

Second Factor Authentication Code

Step 2.

Use Authenticatron_Check to confirm the setup and check time-unique codes at every login.


Authenticatron_Check($Code, $Secret)


$Code is the user input, the code that is generated on their device for authentication. Should be numeric-only in most cases, alpha-numeric if you change some settings.

$Secret is the secret the member scanned that you securely stored for later.

$Variance is an optional integer indicating the adjustment of codes with a 30 second value. Defaults to 2 either side, or 1 minute.


Outputs a boolean value, true if the entered code is within allowed range, false if not.



You only need to check an input is alpha-numeric, and maybe 6 characters long before checking it against a retreieved secret.

$Secret = ...;
if (
	strlen($_POST['secondfactor_code']) == 6 &&
) {
	if ( Authenticatron_Check($_POST['secondfactor_code'], $Secret) ) {
		// Authenticated, log in...
	} else {
		// Incorrect code
} else {
	// Invalid entry


Google Authenticator Icon

Enter the code that your device generates after scanning the image to from Step 1.


Further Reading

Visit our documentation for a more thorough description of the options and functions available to you.

Take a look at the glossary if there are any terms you don't understand.

The server page can be used if this script is installed on your server to check for requirements.

If you're ready to rock, check out the source!

Open Source Initiative Keyhole

This work is predominantly MIT licensed. See the file for more information.