PHP头条
热点:

php中生成随机密码几种方法(1/2)


mt_rand ( int $min , int $max )函数用于生成随机整数,其中$min–$max为ascii码的范围,这里取33-126,可以根据需要调整范围,如ascii码表中97–122位对应a–z的英文字母,具体可参考ascii码表;chr ( int $ascii )函数用于将对应整数$ascii转换成对应的字符。

  代码:

function create_password($pw_length = 8)
{
    $randpwd = '';
    for ($i = 0; $i < $pw_length; $i++)
    {
        $randpwd .= chr(mt_rand(33, 126));
    }
    return $randpwd;
}
// 调用该函数,传递长度参数$pw_length = 6
echo create_password(6);


  方法二:

  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 < $length; $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 2

www.phpzy.comtrue/php/18401.htmlTechArticlephp中生成随机密码几种方法(1/2) mt_rand ( int $min , int $max )函数用于生成随机整数,其中$min$max为ascii码的范围,这里取33-126,可以根据需要调整范围,如ascii码表中97122位对应az的英文字母...

相关文章

    暂无相关文章

PHP之友评论

今天推荐