欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP生成隨機(jī)密碼方法匯總

 更新時(shí)間:2015年08月27日 15:50:31   投稿:mrr  
使用PHP開(kāi)發(fā)應(yīng)用程序,尤其是網(wǎng)站程序,常常需要生成隨機(jī)密碼,如用戶注冊(cè)生成隨機(jī)密碼,用戶重置密碼也需要生成一個(gè)隨機(jī)的密碼,接下來(lái)小編給大家介紹php生成隨機(jī)密碼五種方法,需要的朋友可以參考下

使用PHP開(kāi)發(fā)應(yīng)用程序,尤其是網(wǎng)站程序,常常需要生成隨機(jī)密碼,如用戶注冊(cè)生成隨機(jī)密碼,用戶重置密碼也需要生成一個(gè)隨機(jī)的密碼。隨機(jī)密碼也就是一串固定長(zhǎng)度的字符串,這里我收集整理了幾種生成隨機(jī)字符串的方法,以供大家參考。

方法一:

     1、在 33 – 126 中生成一個(gè)隨機(jī)整數(shù),如 35,

    2、將 35 轉(zhuǎn)換成對(duì)應(yīng)的ASCII碼字符,如 35 對(duì)應(yīng) #

    3、重復(fù)以上 1、2 步驟 n 次,連接成 n 位的密碼

     該算法主要用到了兩個(gè)函數(shù),mt_rand ( int $min , int $max )函數(shù)用于生成隨機(jī)整數(shù),其中 $min – $max 為 ASCII 碼的范圍,這里取 33 -126 ,可以根據(jù)需要調(diào)整范圍,如ASCII碼表中 97 – 122 位對(duì)應(yīng) a – z 的英文字母,具體可參考 ASCII碼表; chr ( int $ascii )函數(shù)用于將對(duì)應(yīng)整數(shù) $ascii 轉(zhuǎn)換成對(duì)應(yīng)的字符。

function create_password($pw_length = 8)
{
 $randpwd = '';
 for ($i = 0; $i < $pw_length; $i++) 
 {
  $randpwd .= chr(mt_rand(33, 126));
 }
 return $randpwd;
}

// 調(diào)用該函數(shù),傳遞長(zhǎng)度參數(shù)$pw_length = 6

echo create_password(6);

方法二:

     1、預(yù)置一個(gè)的字符串 $chars ,包括 a – z,A – Z,0 – 9,以及一些特殊字符

     2、在 $chars 字符串中隨機(jī)取一個(gè)字符

    3、重復(fù)第二步 n 次,可得長(zhǎng)度為 n 的密碼

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

 $password = '';
 for ( $i = 0; $i < $length; $i++ ) 
 {
  // 這里提供兩種字符獲取方式
  // 第一種是使用 substr 截取$chars中的任意一位字符;
  // 第二種是取字符數(shù)組 $chars 的任意元素
  // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
 }

 return $password;
}

方法三:

     1、預(yù)置一個(gè)的字符數(shù)組 $chars ,包括 a – z,A – Z,0 – 9,以及一些特殊字符

     2、通過(guò)array_rand()從數(shù)組 $chars 中隨機(jī)選出 $length 個(gè)元素

     3、根據(jù)已獲取的鍵名數(shù)組 $keys,從數(shù)組 $chars 取出字符拼接字符串。該方法的缺點(diǎn)是相同的字符不會(huì)重復(fù)取。

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 中隨機(jī)取 $length 個(gè)數(shù)組元素鍵名
 $keys = array_rand($chars, $length); 
 $password = '';
 for($i = 0; $i < $length; $i++)
 {
  // 將 $length 個(gè)數(shù)組元素連接成字符串
  $password .= $chars[$keys[$i]];
 }
 return $password;
}

方法四:

本方法是本文被藍(lán)色理想轉(zhuǎn)載后,一名網(wǎng)友提供的一個(gè)新方法,算法簡(jiǎn)單,代碼簡(jiǎn)短,只是因?yàn)閙d5()函數(shù)的返回值的緣故,生成的密碼只包括字母和數(shù)字,不過(guò)也算是一個(gè)不錯(cuò)的方法。算法思想:

     1、time() 獲取當(dāng)前的 Unix 時(shí)間戳

     2、將第一步獲取的時(shí)間戳進(jìn)行 md5() 加密

    3、將第二步加密的結(jié)果,截取 n 位即得想要的密碼

function get_password( $length = 8 ) 
{
 $str = substr(md5(time()), 0, 6);
 return $str;
}

時(shí)間效率對(duì)比

     我們使用以下PHP代碼,計(jì)算上面的 4 個(gè)隨機(jī)密碼生成函數(shù)生成 6 位密碼的運(yùn)行時(shí)間,進(jìn)而對(duì)他們的時(shí)間效率進(jìn)行一個(gè)簡(jiǎn)單的對(duì)比。

<?php
function getmicrotime()
{
 list($usec, $sec) = explode(" ",microtime());
 return ((float)$usec + (float)$sec);
}
// 記錄開(kāi)始時(shí)間
$time_start = getmicrotime();
// 這里放要執(zhí)行的PHP代碼,如:
// echo create_password(6);
// 記錄結(jié)束時(shí)間
$time_end = getmicrotime();
$time = $time_end - $time_start;
 // 輸出運(yùn)行總時(shí)間 
echo "執(zhí)行時(shí)間 $time seconds";
?>

方法五:

function rand_string($len = 16, $keyword = '') {
 if (strlen($keyword) > $len) {//關(guān)鍵字不能比總長(zhǎng)度長(zhǎng)
  return false;
 }
 $str = '';
 $chars = 'abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHIJKMNPQRSTUVWXYZ'; //去掉1跟字母l防混淆   
 if ($len > strlen($chars)) {//位數(shù)過(guò)長(zhǎng)重復(fù)字符串一定次數(shù)
  $chars = str_repeat($chars, ceil($len / strlen($chars)));
 }
 $chars = str_shuffle($chars); //打亂字符串
 $str = substr($chars, 0, $len);
 if (!empty($keyword)) {
  $start = $len - strlen($keyword);
  $str = substr_replace($str, $keyword, mt_rand(0, $start), strlen($keyword)); //從隨機(jī)位置插入關(guān)鍵字
 }
 return $str;
}

echo rand_string(16,"ab"); //output example:V8bNY6SmkeywordB
?>

最終得出的結(jié)果是:

    方法一:9.8943710327148E-5 秒

     方法二:9.6797943115234E-5 秒

     方法三:0.00017499923706055 秒

     方法四:3.4093856811523E-5 秒

     可以看出方法一和方法二的執(zhí)行時(shí)間都差不多,方法四運(yùn)行時(shí)間最短,而方法三的運(yùn)行時(shí)間稍微長(zhǎng)點(diǎn)。

相關(guān)文章

最新評(píng)論