php產生隨機密碼函數(四款)

來源:互聯網
上載者:User

方法一

 代碼如下 複製代碼

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;
}

方法二

動產生6位元字、字母 混合密碼

 代碼如下 複製代碼
<?php
$str = "0123456789abcdefghijklmnopqrstuvwxyz";   //   輸出字元集 
$n = 6;   //   輸出串長度 
$len = strlen($str)-1;
for($j=0 ; $j<200 ; $j++){
for($i=0 ; $i<$n; $i++){
    $s .=  $str[rand(0,$len)]; 
}
echo $s . "<br/>";
$s = "";
}
?>
自動產生數字、字母、符號的密碼
<?php
      $a = "12345678";
      $b = "abcdefghijklmnopqistuvwxyz";
      $s = substr(str_shuffle($a), 0, 2);
      $e = substr(str_shuffle($b), 0, 2);
      echo $s . substr(str_shuffle("!@#$%^&*"), 0, 2) . $e;
?>

方法三

 代碼如下 複製代碼

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);


方法三

 代碼如下 複製代碼

<?php
function getmicrotime()
{
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}
 
// 記錄開始時間
$time_start = getmicrotime();
   
// 這裡放要執行的php代碼,如:
// echo create_password(6);
 
// 記錄結束時間
$time_end = getmicrotime();
$time = $time_end - $time_start;

 // 輸出運行總時間
echo "執行時間 $time seconds";
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.