如何在 PHP 中使用 TOTP / HOTP 库

How to use TOTP / HOTP library in PHP

我试图实现 TOTP PHP 库作为我登录表单的另一种身份验证。我从 github 下载并按照以下代码安装说明:

<?php
include('src/Factory.php');
include('src/HOTP.php');
include('src/HOTPInterface.php');
include('src/OTP.php');
include('src/OTPInterface.php');
include('src/ParameterTrait.php');
include('src/TOTP.php');
include('src/TOTPInterface.php');
use OTPHP\TOTP;

$otp = TOTP::create();
echo 'The current OTP is: '.$otp->now();
?>

然而,我得到了错误消息 Warning: Unsupported declare 'strict_types' in C:\wamp64\www\otp\src\HOTP.php on line 3Parse error: syntax error, unexpected '?', expecting variable (T_VARIABLE) in C:\wamp64\www\otp\src\HOTP.php on line 28

我不明白为什么会这样。非常感谢您的帮助。谢谢。

尝试 multiOTP class,它与 PHP 5.x 兼容 https://github.com/multiOTP/multiotp

安装较低版本的软件包,如 ~8.0,可与 PHP 5.5

配合使用

https://github.com/Spomky-Labs/otphp/blob/v8.3.2/composer.json#L19

将此添加到您的 composer.json

"require": { "spomky-labs/otphp": "~8.3" }

或使用此 link 以 ZIP 格式下载并手动将其添加到您的项目中

https://github.com/Spomky-Labs/otphp/archive/v8.3.2.zip

更好的是使用作曲家,因为你有自动加载器。 :) 这样就不需要手动要求文件了。