phpone-time-password

How to use TOTP / HOTP library in PHP


I tried to implement TOTP PHP library as another authentication for my login form. I downloaded and followed installation instruction from github as folowing code:

<?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();
?>

Yet, I ended up with error message Warning: Unsupported declare 'strict_types' in C:\wamp64\www\otp\src\HOTP.php on line 3 and Parse error: syntax error, unexpected '?', expecting variable (T_VARIABLE) in C:\wamp64\www\otp\src\HOTP.php on line 28.

I could not figure out why it was that.


Solution

  • Install lower version of package like ~8.0, which works with PHP 5.5

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

    add this in your composer.json

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

    Or use this link to download it as ZIP and add it to your project manually

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

    Better is to use composer, because you have auto autoloader. :) That way there is no need to manually require files.