Home  >  Article  >  Backend Development  >  php中生成随机密码的自定义函数代码_PHP

php中生成随机密码的自定义函数代码_PHP

WBOY
WBOYOriginal
2016-06-01 11:59:27740browse

代码一:

生成一个随机密码的函数,生成的密码为小写字母与数字的随机字符串,长度可自定义。相对来说,这个比较简单

复制代码 代码如下:
/*
 * php自动生成新密码自定义函数(带实例演示)
      适用环境: PHP5.2.x  / mysql 5.0.x
* */
function genPassword($min = 5, $max = 8) 

    $validchars="abcdefghijklmnopqrstuvwxyz123456789"; 
    $max_char=strlen($validchars)-1; 
    $length=mt_rand($min,$max); 
    $password = ""; 
    for($i=0;$i    { 
        $password.=$validchars[mt_rand(0,$max_char)]; 
    } 
        return $password; 
    } 
    echo "新密码:".genPassword()."
"; 
    echo "新密码:".genPassword(5,10)."
";
?>


下面总结了一些实例各位朋友可参考。

例1

最简洁的生成方法

复制代码 代码如下:
function generatePassword($length=8)
{
    $chars = array_merge(range(0,9),
                     range('a','z'),
                     range('A','Z'),
                     array('!','@','$','%','^','&','*'));
    shuffle($chars);
    $password = '';
    for($i=0; $i        $password .= $chars[$i];
    }
    return $password;
}

例2

1、在 33 – 126 中生成一个随机整数,如 35,
2、将 35 转换成对应的ASCII码字符,如 35 对应 #
3、重复以上 1、2 步骤 n 次,连接成 n 位的密码

复制代码 代码如下:
function create_password($pw_length = 8)
{
    $randpwd = '';
    for ($i = 0; $i     {
        $randpwd .= chr(mt_rand(33, 126));
    }
    return $randpwd;
}

// 调用该函数,传递长度参数$pw_length = 6
echo create_password(6);

实例

复制代码 代码如下:
mt_srand((double) microtime() * 1000000);

function gen_random_password($password_length = 32, $generated_password = ""){
 $valid_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 $chars_length = strlen($valid_characters) - 1;
 for($i = $password_length; $i--; ) {
  //$generated_password .= $valid_characters[mt_rand(0, $chars_length)];

  $generated_password .= substr($valid_characters, (mt_rand()%(strlen($valid_characters))), 1);
 }
 return $generated_password;
}

?>


php 密码生成器 v 4.0



密码生成器v4.0 by freemouse



if (isset($_GET['password_length'])){
 if(preg_match("/([0-9]{1,8})/", $_GET['password_length'])){
  print("密码生成成功:

" . gen_random_password($_GET['password_length']) . "

n");
 } else {
  print("密码长度不正确!

n");
 }
}

print 请为密码生成其指定生成密码的长度:



 
 

end;

?>


例4


1、预置一个的字符串 $chars ,包括 a – z,A – Z,0 – 9,以及一些特殊字符
2、在 $chars 字符串中随机取一个字符
3、重复第二步 n 次,可得长度为 n 的密码

复制代码 代码如下:
function generate_password( $length = 8 ) {
    // 密码字符集,可任意添加你需要的字符
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}~`+=,.;:/?|';

    $password = '';
    for ( $i = 0; $i     {
        // 这里提供两种字符获取方式
        // 第一种是使用 substr 截取$chars中的任意一位字符;
        // 第二种是取字符数组 $chars 的任意元素
        // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }

    return $password;
}

上面经过测试性能都不如下面这个

1、预置一个的字符数组 $chars ,包括 a – z,A – Z,0 – 9,以及一些特殊字符
2、通过array_rand()从数组 $chars 中随机选出 $length 个元素
3、根据已获取的键名数组 $keys,从数组 $chars 取出字符拼接字符串。该方法的缺点是相同的字符不会重复取。

复制代码 代码如下:
function make_password( $length = 8 )
{
    // 密码字符集,可任意添加你需要的字符
    $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
    'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
    't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D',
    'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O',
    'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!',
    '@','#', '$', '%', '^', '&', '*', '(', ')', '-', '_',
    '[', ']', '{', '}', '', '~', '`', '+', '=', ',',
    '.', ';', ':', '/', '?', '|');

    // 在 $chars 中随机取 $length 个数组元素键名
    $keys = array_rand($chars, $length);

    $password = '';
    for($i = 0; $i     {
        // 将 $length 个数组元素连接成字符串
        $password .= $chars[$keys[$i]];
    }

    return $password;
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn