首頁 > 後端開發 > php教程 > PHP產生隨機密碼的4種方法及效能對比

PHP產生隨機密碼的4種方法及效能對比

WBOY
發布: 2016-08-08 09:23:46
原創
1071 人瀏覽過

使用PHP開發應用程序,尤其是網站程序,常常需要產生隨機密碼,如用戶註冊生成隨機密碼,用戶重置密碼也需要產生一個隨機的密碼。隨機密碼也就是一串固定長度的字串,這裡我收集整理了幾種產生隨機字串的方法,以供大家參考。

PHP產生隨機密碼的4種方法及效能對比

方法一:

1、在33 – 126 中產生一個隨機整數,如35,

2、將35 轉換成對應的ASCII碼字符,如35 對應

2、將35 轉換成對應的ASCII碼字符,如35 對應

331 、2 步驟n 次,連接成n 位的密碼

該演算法主要用到了兩個函數,mt_rand ( int $min , int $max )函數用於產生隨機整數,其中$min – $max 為ASCII 碼的範圍,這裡取33 -126 ,可以根據需要調整範圍,如ASCII碼表中97 – 122 位元對應a – z 的英文字母,具體可參考ASCII碼表; chr ( int $ascii )函數用於將對應整數$ascii 轉換成對應的字元。

view sourceprint?

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

方法二:

1、預置一個的字串$chars ,包括a – z,A – Z,0 – 99,以及一些特殊字符、在$chars 字串中隨機取一個字元

3、重複第二步n 次,可得長度為n 的密碼

view sourceprint?

<ol><li><span><span>function</span><span> generate_password( </span><span>$length</span><span> = 8 ) { </span></span></li><li><span>    <span>// 密码字符集,可任意添加你需要的字符</span><span> </span></span></li><li><span>    <span>$chars</span><span> = </span><span>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|'</span><span>; </span></span></li>
<li><span> </span></li>
<li><span>    <span>$password</span><span> = </span><span>''</span><span>; </span></span></li>
<li><span>    <span>for</span><span> ( </span><span>$i</span><span> = 0; </span><span>$i</span><span> < </span><span>$length</span><span>; </span><span>$i</span><span>++ )  </span></span></li><li><span>    { </span></li><li><span>        <span>// 这里提供两种字符获取方式</span><span> </span></span></li><li><span>        <span>// 第一种是使用 substr 截取$chars中的任意一位字符;</span><span> </span></span></li><li><span>        <span>// 第二种是取字符数组 $chars 的任意元素</span><span> </span></span></li><li><span>        <span>// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);</span><span> </span></span></li><li><span>        <span>$password</span><span> .= </span><span>$chars</span><span>[ mt_rand(0, </span><span>strlen</span><span>(</span><span>$chars</span><span>) - 1) ]; </span></span></li><li><span>    } </span></li><li><span> </span></li><li><span>    <span>return</span><span> </span><span>$password</span><span>; </span></span></li><li><span>} </span></li></ol>
登入後複製

預置一個的字元陣列$chars ,包括a – z,A – Z,0 – 9,以及一些特殊字元

2、透過array_rand()從陣列$chars 中隨機選出$length 個元素

3、根據已取得的鍵名數組$keys,從數組$chars 取出字元拼接字串。此方法的缺點是相同的字元不會重複取。

view sourceprint?

<ol><li><span><span>function</span><span> make_password( </span><span>$length</span><span> = 8 ) </span></span></li><li><span>{ </span></li><li><span>    <span>// 密码字符集,可任意添加你需要的字符</span><span> </span></span></li><li><span>    <span>$chars</span><span> = </span><span>array</span><span>(</span><span>'a'</span><span>, </span><span>'b'</span><span>, </span><span>'c'</span><span>, </span><span>'d'</span><span>, </span><span>'e'</span><span>, </span><span>'f'</span><span>, </span><span>'g'</span><span>, </span><span>'h'</span><span>,  </span></span></li><li><span>    <span>'i'</span><span>, </span><span>'j'</span><span>, </span><span>'k'</span><span>, </span><span>'l'</span><span>,</span><span>'m'</span><span>, </span><span>'n'</span><span>, </span><span>'o'</span><span>, </span><span>'p'</span><span>, </span><span>'q'</span><span>, </span><span>'r'</span><span>, </span><span>'s'</span><span>,  </span></span></li><li><span>    <span>'t'</span><span>, </span><span>'u'</span><span>, </span><span>'v'</span><span>, </span><span>'w'</span><span>, </span><span>'x'</span><span>, </span><span>'y'</span><span>,</span><span>'z'</span><span>, </span><span>'A'</span><span>, </span><span>'B'</span><span>, </span><span>'C'</span><span>, </span><span>'D'</span><span>,  </span></span></li><li><span>    <span>'E'</span><span>, </span><span>'F'</span><span>, </span><span>'G'</span><span>, </span><span>'H'</span><span>, </span><span>'I'</span><span>, </span><span>'J'</span><span>, </span><span>'K'</span><span>, </span><span>'L'</span><span>,</span><span>'M'</span><span>, </span><span>'N'</span><span>, </span><span>'O'</span><span>,  </span></span></li><li><span>    <span>'P'</span><span>, </span><span>'Q'</span><span>, </span><span>'R'</span><span>, </span><span>'S'</span><span>, </span><span>'T'</span><span>, </span><span>'U'</span><span>, </span><span>'V'</span><span>, </span><span>'W'</span><span>, </span><span>'X'</span><span>, </span><span>'Y'</span><span>,</span><span>'Z'</span><span>,  </span></span></li><li><span>    <span>'0'</span><span>, </span><span>'1'</span><span>, </span><span>'2'</span><span>, </span><span>'3'</span><span>, </span><span>'4'</span><span>, </span><span>'5'</span><span>, </span><span>'6'</span><span>, </span><span>'7'</span><span>, </span><span>'8'</span><span>, </span><span>'9'</span><span>, </span><span>'!'</span><span>,  </span></span></li><li><span>    <span>'@'</span><span>,</span><span>'#'</span><span>, </span><span>'$'</span><span>, </span><span>'%'</span><span>, </span><span>'^'</span><span>, </span><span>'&'</span><span>, </span><span>'*'</span><span>, </span><span>'('</span><span>, </span><span>')'</span><span>, </span><span>'-'</span><span>, </span><span>'_'</span><span>,  </span></span></li><li><span>    <span>'['</span><span>, </span><span>']'</span><span>, </span><span>'{'</span><span>, </span><span>'}'</span><span>, </span><span>'<'</span><span>, </span><span>'>'</span><span>, </span><span>'~'</span><span>, </span><span>'`'</span><span>, </span><span>'+'</span><span>, </span><span>'='</span><span>, </span><span>','</span><span>,  </span></span></li>
<li><span>    <span>'.'</span><span>, </span><span>';'</span><span>, </span><span>':'</span><span>, </span><span>'/'</span><span>, </span><span>'?'</span><span>, </span><span>'|'</span><span>); </span></span></li>
<li><span> </span></li>
<li><span>    <span>// 在 $chars 中随机取 $length 个数组元素键名</span><span> </span></span></li>
<li><span>    <span>$keys</span><span> = </span><span>array_rand</span><span>(</span><span>$chars</span><span>, </span><span>$length</span><span>);  </span></span></li>
<li><span> </span></li>
<li><span>    <span>$password</span><span> = </span><span>''</span><span>; </span></span></li>
<li><span>    <span>for</span><span>(</span><span>$i</span><span> = 0; </span><span>$i</span><span> < </span><span>$length</span><span>; </span><span>$i</span><span>++) </span></span></li><li><span>    { </span></li><li><span>        <span>// 将 $length 个数组元素连接成字符串</span><span> </span></span></li><li><span>        <span>$password</span><span> .= </span><span>$chars</span><span>[</span><span>$keys</span><span>[</span><span>$i</span><span>]]; </span></span></li><li><span>    } </span></li><li><span> </span></li><li><span>    <span>return</span><span> </span><span>$password</span><span>; </span></span></li><li><span>} </span></li></ol>
登入後複製

方法四:

本方法是本文被藍色理想轉載後,一位網友提供的一個新方法,簡單,

本方法是本文被藍色理想轉載後,一位網友提供的一個新方法,只是簡單,紙,只是因為簡單(md)函數的回傳值的緣故,產生的密碼只包括字母和數字,不過也算是不錯的方法。演算法思想:

1、time() 取得目前的Unix 時間戳

2、將第一步取得的時間戳進行md5() 加密

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

view sourceprint?

<ol><li><span><span>function</span><span> get_password( </span><span>$length</span><span> = 8 )  </span></span></li><li><span>{ </span></li><li><span>    <span>$str</span><span> = </span><span>substr</span><span>(md5(time()), 0, 6); </span></span></li><li><span>    <span>return</span><span> </span><span>$str</span><span>; </span></span></li><li><span>} </span></li></ol>
登入後複製

時間效率對比

我們使用以下PHP程式碼,計算上面的4 個隨機密碼產生6 位密碼的運行時間,進6 位密碼的運行時間,而對他們的4 個隨機密碼產生6 位密碼的運行時間,進6 位元密碼的運行時間,而對他們的4 個隨機密碼生成時間效率進行一個簡單的比較。

view sourceprint?

<ol><li><span><span><?php </span></span></li><li><span><span>function</span><span> getmicrotime() </span></span></li><li><span>{ </span></li><li><span>    list(<span>$usec</span><span>, </span><span>$sec</span><span>) = </span><span>explode</span><span>(</span><span>" "</span><span>,microtime()); </span></span></li><li><span>    <span>return</span><span> ((float)</span><span>$usec</span><span> + (float)</span><span>$sec</span><span>); </span></span></li><li><span>} </span></li><li><span> </span></li><li><span><span>// 记录开始时间</span><span> </span></span></li><li><span><span>$time_start</span><span> = getmicrotime(); </span></span></li><li><span> </span></li><li><span><span>// 这里放要执行的PHP代码,如:</span><span> </span></span></li><li><span><span>// echo create_password(6);</span><span> </span></span></li><li><span> </span></li><li><span><span>// 记录结束时间</span><span> </span></span></li><li><span><span>$time_end</span><span> = getmicrotime(); </span></span></li><li><span><span>$time</span><span> = </span><span>$time_end</span><span> - </span><span>$time_start</span><span>; </span></span></li><li><span> </span></li><li><span> <span>// 输出运行总时间 </span><span> </span></span></li><li><span><span>echo</span><span> </span><span>"执行时间 $time seconds"</span><span>; </span></span></li><li><span>?> </span></span></li>
</ol>
登入後複製

最終得出的結果是:

方法一:9.8943710327148E-5575357575753253535方法三:0.00017499923706055 秒

方法四:3.4093856811523E-5 秒

可以看出方法一和方法二的執行時間都差不多,方法四運行時間最短,而方法三的運行時間稍微長點。

以上就介紹了PHP產生隨機密碼的4種方法及效能對比,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板