如何用PHP生成随机密码? [英] How to generate random password with PHP?
本文介绍了如何用PHP生成随机密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
还是有自动生成随机密码的软件?
Or is there a software to auto generate random passwords?
推荐答案
只需构建一个随机的a-z
,A-Z
,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.
这篇关于如何用PHP生成随机密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文