php access Google Authenticator

If it is unclear Google Authenticator is something, it is recommended to go to find out, back to see this article, we will be better understanding of some.

There php of Google Auth GitHub implemented on the package, with direct win here, available at the following address itself GitHub Google Authenticator

To use Google Auth will need to do its association supports two related ways are "scan code" and "keys", has complete support.

<?php
require_once "../GoogleAuthenticator/PHPGangsta/GoogleAuthenticator.php";
$obj = new PHPGangsta_GoogleAuthenticator();
//在这里生成秘钥,如果使用用户输入秘钥的关联方式,就将这段秘钥展示给用户
$sec = $obj->createSecret();
//如果使用扫码的方式进行关联,就可以使用下面的方法来生成二维码展示给用户
$url = $obj->getQRCodeGoogleUrl('liangcs2', $sec);

The first argument getQRCodeGoogleUrl method, the information is displayed in Google Auth of the association, as follows:
Here Insert Picture Description
In this way, the account associated with has been created.
At this time, when the user during login, you can enter the six dynamic code Google Auth supplied to the two-step verification.

<?php
require_once "../GoogleAuthenticator/PHPGangsta/GoogleAuthenticator.php";
$obj = new PHPGangsta_GoogleAuthenticator();
$dyncCode = $_POST['dynamic_code'];
//对用户输入的6位动态码进行校验,参数$sec是与此用户关联的秘钥
$ret = $obj->verifyCode($sec, $dyncCode);
if($ret) {
    echo "login success\n";
} else {
    echo "login fail\n";
}

So far, php and access Google Auth is complete, contains a relatively complete "introduced -> Generate -> Link -> check" operation, and then do some adaptation based on actual business needs, we will be able to significantly enhanced account of the security system.

Published 105 original articles · won praise 58 · views 410 000 +

Guess you like

Origin blog.csdn.net/ljl890705/article/details/104934167