我要求为我的用户提供生成安全密码的功能......
我脑子里有算法,但不知道怎么处理它..
最后一部分是我无法实现的..
任何帮助将不胜感激......
function randompassword() { // Declaring the function
//Config Start
$salt = "abchefghjkmnpqrstuvwxyz0123456789"; // Your characters bank
$n = "8"; // Number of character to return
//end config
//Logic for password generation..
return $pass;
}
答案 0 :(得分:3)
$v = strlen($salt);
$pass='';
srand((double)microtime()*1000000);
$i = 1;
while ($i <= $n) {
$num = rand() % $v;
$tmp = substr($salt, $num, 1);
$pass = $pass . $tmp;
$i++;
}
您可以尝试此代码..
答案 1 :(得分:2)
你可以使用这样的东西来管理你的密码强度,并自定义你想要输入密码的字符。
function randomPassword($length=9, $strength=0) {
$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;
}
答案 2 :(得分:2)
<?php
$salt = "abchefghjkmnpqrstuvwxyz0123456789";
for ($i=0;$i<$n;$i++)
$pass=$pass.$salt[rand(0,strlen($salt))];
echo $pass;
?>
您最后还可以添加substr(sha1($pass),0,$n);
答案 3 :(得分:1)
这真的不那么复杂:
$characters = 'abcd...';
$password = '';
$length = 8;
while (strlen($password) < $length) {
$password .= $characters[mt_rand(0, strlen($characters) - 1)];
}
答案 4 :(得分:1)
试试这个:
function unique_id2()
{
return substr(md5(uniqid(rand(), true)), 25);
}
答案 5 :(得分:0)
<?php
$password = "";
for($k=0;$k<$n;$k++){
$rand = rand(0,strlen($salt));
$password .= $salt[$rand];
}
echo $pass;
?>