如何使用 PHP 生成随机密码?
或者有没有软件可以自动生成随机密码?
Or is there a software to auto generate random passwords?
推荐答案
随便构建一串随机的az,AZ,0-9(或任何你想要的)直到所需的长度.下面是一个 PHP 示例:
Just build a string of random a-z, A-Z, 0-9 (or whatever you want) up to the desired length. Here's an example in PHP:
function generatePassword($length = 8) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$count = mb_strlen($chars);
for ($i = 0, $result = ''; $i < $length; $i++) {
$index = rand(0, $count - 1);
$result .= mb_substr($chars, $index, 1);
}
return $result;
}
为了优化,你可以在方法(或父类)中将$chars定义为一个静态变量或常量,如果你要在这个过程中多次调用这个函数一次执行.
To optimize, you can define $chars as a static variable or constant in the method (or parent class) if you'll be calling this function many times during a single execution.
相关文章