Bra*_*tie 151
$rand = substr(md5(microtime()),rand(0,26),5);
Run Code Online (Sandbox Code Playgroud)
这是我最好的猜测 - 除非你也在寻找特殊角色:
$seed = str_split('abcdefghijklmnopqrstuvwxyz'
.'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.'0123456789!@#$%^&*()'); // and any other characters
shuffle($seed); // probably optional since array_is randomized; this may be redundant
$rand = '';
foreach (array_rand($seed, 5) as $k) $rand .= $seed[$k];
Run Code Online (Sandbox Code Playgroud)
并且,对于基于时钟的一个(由于它是增量的,更少的冲突):
function incrementalHash($len = 5){
$charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$base = strlen($charset);
$result = '';
$now = explode(' ', microtime())[1];
while ($now >= $base){
$i = $now % $base;
$result = $charset[$i] . $result;
$now /= $base;
}
return substr($result, -5);
}
Run Code Online (Sandbox Code Playgroud)
注意:增量意味着更容易猜测; 如果您将此作为盐或验证令牌使用,请不要."WCWyb"的盐(现在)意味着从现在起5秒钟它的"WCWyg")
Mat*_*hew 68
如果for循环供应短缺,这是我喜欢使用的:
$s = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 5)), 0, 5);
Run Code Online (Sandbox Code Playgroud)
Joh*_*ker 25
一种快速的方法是使用uniqid函数中最易变的字符.
例如:
$rand = substr(uniqid('', true), -5);
Run Code Online (Sandbox Code Playgroud)
小智 21
您可以像这样尝试:
$length = 5;
$randomletter = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, $length);
Run Code Online (Sandbox Code Playgroud)
更多详情:http://forum.arnlweb.com/viewtopic.php?f = 7&t = 25
Arc*_*dix 14
以下应该提供最少的重复机会(您可能希望替换mt_rand()为更好的随机数源,例如/dev/*random来自GUID或来自GUID):
<?php
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$result = '';
for ($i = 0; $i < 5; $i++)
$result .= $characters[mt_rand(0, 61)];
?>
Run Code Online (Sandbox Code Playgroud)
编辑:
如果你担心安全问题,真的,不使用rand()或mt_rand()并验证您的随机数据设备实际上是一个设备生成随机数据,而不是一个普通的文件或预测的东西一样/dev/zero.mt_rand()认为有害:https:
//spideroak.com/blog/20121205114003-exploit-information-leaks-in-random-numbers-from-python-ruby-and-php
编辑:
如果你有PHP的OpenSSL支持,你可以使用openssl_random_pseudo_bytes():
<?php
$length = 5;
$randomBytes = openssl_random_pseudo_bytes($length);
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$charactersLength = strlen($characters);
$result = '';
for ($i = 0; $i < $length; $i++)
$result .= $characters[ord($randomBytes[$i]) % $charactersLength];
?>
Run Code Online (Sandbox Code Playgroud)
我总是使用相同的功能,通常生成密码.它易于使用且有用.
function randPass($length, $strength=8) {
$vowels = 'aeuy';
$consonants = 'bdghjmnpqrstvz';
if ($strength >= 1) {
$consonants .= 'BDGHJLMNPQRSTVWXZ';
}
if ($strength >= 2) {
$vowels .= "AEUY";
}
if ($strength >= 4) {
$consonants .= '23456789';
}
if ($strength >= 8) {
$consonants .= '@#$%';
}
$password = '';
$alt = time() % 2;
for ($i = 0; $i < $length; $i++) {
if ($alt == 1) {
$password .= $consonants[(rand() % strlen($consonants))];
$alt = 0;
} else {
$password .= $vowels[(rand() % strlen($vowels))];
$alt = 1;
}
}
return $password;
}
Run Code Online (Sandbox Code Playgroud)
似乎str_shuffle对此很有用.用你想要的任何一个字符播种洗牌.
$my_rand_strng = substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), -5);
Run Code Online (Sandbox Code Playgroud)
小智 5
我也不知道如何做到这一点,直到我想到使用 PHP array。我很确定这是用数组生成随机字符串或数字的最简单方法。代码:
function randstr ($len=10, $abc="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789") {
$letters = str_split($abc);
$str = "";
for ($i=0; $i<=$len; $i++) {
$str .= $letters[rand(0, count($letters)-1)];
};
return $str;
};
Run Code Online (Sandbox Code Playgroud)
你可以像这样使用这个函数
randstr(20) // returns a random 20 letter string
// Or like this
randstr(5, abc) // returns a random 5 letter string using the letters "abc"
Run Code Online (Sandbox Code Playgroud)