Php函数调用

问题描述 投票:-1回答:1

我有这个功能:

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参数。谢谢 :)

php
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.