使用PHP调用类方法时会出错



我在使用PHP调用一个类方法时会遇到错误。我的代码在下面。

user.php:

require_once ('common.php');
$userClass=new CommonUtilFuncs();
$login_code =$userClass->getToken(6);
echo $login_code;

common.php:

class CommonUtilFuncs{
   function __construct() {
    }
    // destructor
    function __destruct() {
        // $this->close();
    }
    public function getToken($length){
        $token = "";
        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
        $codeAlphabet.= "0123456789";
        $max = strlen($codeAlphabet); // edited
        $crypto=$this->crypto_rand_secure(0, $max - 1);
        for ($i = 0; $i < $length; $i++) {
            $token .= $codeAlphabet[$crypto];
        }
        return $token;
    }
    private function crypto_rand_secure($min, $max) {
        $range = $max - $min;
        if ($range < 1)
            return $min; // not so random...
        $log = ceil(log($range, 2));
        $bytes = (int) ($log / 8) + 1; // length in bytes
        $bits = (int) $log + 1; // length in bits
        $filter = (int) (1 << $bits) - 1; // set all lower bits to 1
        do {
            $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
            $rnd = $rnd & $filter; // discard irrelevant bits
        } while ($rnd > $range);
        return $min + $rnd;
    }
}

user.php页面中,我正在尝试打印登录代码,但没有结果。The page is not working错误即将到来。

更改文件名
common.php

to

CommonUtilFuncs.php

,还将require_once ('common.php')更改为require_once ('CommonUtilFuncs.php');

最新更新