php中的random无法使用,php – / dev / urandom错误(webhost拒绝权限)

我正在使用功能:

private function random($len) {

if (@is_readable('/dev/urandom')) {

$f=fopen('/dev/urandom', 'r');

$urandom=fread($f, $len);

fclose($f);

}

$return='';

for ($i=0;$i

if (!isset($urandom)) {

if ($i%2==0) mt_srand(time()%2147 * 1000000 + (double)microtime() * 1000000);

$rand=48+mt_rand()%64;

} else $rand=48+ord($urandom[$i])%64;

if ($rand>57)

$rand+=7;

if ($rand>90)

$rand+=6;

if ($rand==123) $rand=52;

if ($rand==124) $rand=53;

$return.=chr($rand);

}

return $return;

}

我有一些触发此功能的表单,我收到错误:

int(2) string(200) “is_readable(): open_basedir restriction in effect.

File(/dev/urandom) is not within the allowed path(s):

有没有办法替换这个函数而不使用/ dev / urandom?

非常感谢你.

解决方法:

从(以前接受的)答案:

Instead of urandom you can use “rand”:

Nooooooooo!

处理open_basedir是我们在random_compat中优雅处理的事情之一.认真考虑导入该库然后只使用random_bytes()而不是从/ dev / urandom读取.

此外,如果您需要一个函数来生成随机字符串(取决于PHP 7或random_compat):

/**

* Note: See https://paragonie.com/b/JvICXzh_jhLyt4y3 for an alternative implementation

*/

function random_string($length = 26, $alphabet = 'abcdefghijklmnopqrstuvwxyz234567')

{

if ($length < 1) {

throw new InvalidArgumentException('Length must be a positive integer');

}

$str = '';

$alphamax = strlen($alphabet) - 1;

if ($alphamax < 1) {

throw new InvalidArgumentException('Invalid alphabet');

}

for ($i = 0; $i < $length; ++$i) {

$str .= $alphabet[random_int(0, $alphamax)];

}

return $str;

}

标签:php,random,code-generation

来源: https://codeday.me/bug/20190717/1488541.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值