php函数调用

时间:2018-09-17 18:53:02

标签: php

我有这个功能:

public function getCode($secret, $timeSlice = null)
{
    if ($timeSlice === null) {
        $timeSlice = floor(time() / 30);
    }

    $secretkey = $this->_base32Decode($secret);

    // Pack time into binary string
    $time = chr(0).chr(0).chr(0).chr(0).pack('N*', $timeSlice);
    // Hash it with users secret key
    $hm = hash_hmac('SHA1', $time, $secretkey, true);
    // Use last nipple of result as index/offset
    $offset = ord(substr($hm, -1)) & 0x0F;
    // grab 4 bytes of the result
    $hashpart = substr($hm, $offset, 4);

    // Unpak binary value
    $value = unpack('N', $hashpart);
    $value = $value[1];
    // Only 32 bits
    $value = $value & 0x7FFFFFFF;

    $modulo = pow(10, $this->_codeLength);

    return str_pad($value % $modulo, $this->_codeLength, '0', STR_PAD_LEFT);
}

从秘密中给我2FA代码, 但我不知道如何在另一页上调用该函数以及如何配置$timeslice参数。谢谢:)

1 个答案:

答案 0 :(得分:0)

要从其他文件中调用该函数,需要先includerequire该文件,然后才能启动调用。 之后,您可以像下面这样调用函数(假设您要在$ timeSlice变量中输入300):

require 'functions_file.php';
$The_code = getCode("this is a secret", 300);

如果函数是类的一部分,则必须实例化该类(假设该类名为FunctionClass):

require 'functions_file.php';
$Instantiated_Class = new FunctionClass();
$The_code = $Instantiated_Class->getCode("this is a secret", 300);

或者您可以:

require 'functions_file.php';
$The_code = FunctionClass::getCode("this is a secret", 300);