使用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
错误即将来临。
答案 0 :(得分:-1)
从
更改文件名common.php
到
CommonUtilFuncs.php
并将require_once ('common.php')
更改为require_once ('CommonUtilFuncs.php')
;