用于微信小程序接口开发中生成3rd_session。
/**
* 生成随机标识
* @param $len
* @return mixed|string
*/
public function randomFromDev($len) {
$fp = @fopen('/dev/urandom','rb');
$result = '';
if ($fp !== FALSE) {
$result .= @fread($fp, $len);
@fclose($fp);
}else{
trigger_error('Can not open /dev/urandom.');
}
if (empty($result)) $result = md5(mt_rand(111111111,999999999)); //兼容
$result = base64_encode($result); // convert from binary to string
$result = strtr($result, '+/', '-_'); // remove none url chars
return substr($result, 0, $len);
}