溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP如何生成隨機(jī)密碼

發(fā)布時(shí)間:2021-07-02 15:35:09 來(lái)源:億速云 閱讀:125 作者:chen 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容主要講解“PHP如何生成隨機(jī)密碼”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“PHP如何生成隨機(jī)密碼”吧!

類代碼:

<?php
/**
 * PHP - Password Generator Class
 * Version 1.0.0
 *
 */
 
if (@!is_object($passGen) || !isset($passGen)) {
  $passGen = new Password;
}
 
class Password
{
 
  /**
   * 大寫(xiě)字母 A-Z
   *
   * @var array
   */
  protected $uppercase_chars;
 
  /**
   * 小寫(xiě)字母 a-z
   *
   * @var array
   */
  protected $lowercase_chars;
 
  /**
   * 阿拉伯?dāng)?shù)字 0-9
   *
   * @var array
   */
  protected $number_chars;
 
  /**
   * 特殊字符
   *
   * @var array
   */
  protected $special_chars;
 
  /**
   * 其他特殊字符
   *
   * @var array
   */
  protected $extra_chars;
 
  /**
   * 最終用來(lái)生成密碼的所有字符
   *
   * @var array
   */
  protected $chars = array();
 
  /**
   * 密碼長(zhǎng)度
   *
   * @var array
   */
  public $length;
 
  /**
   * 是否使用大寫(xiě)字母
   *
   * @var boolean
   */
  public $uppercase;
 
  /**
   * 是否使用小寫(xiě)字母
   *
   * @var boolean
   */
  public $lowercase;
 
  /**
   * 是否使用阿拉伯?dāng)?shù)字
   *
   * @var boolean
   */
  public $number;
 
  /**
   * 是否使用特殊字符
   *
   * @var boolean
   */
  public $special;
 
  /**
   * 是否使用額外的特殊字符
   *
   * @var boolean
   */
  public $extra;
 
  /**
   * 初始化密碼設(shè)置
   *
   * @param int $length
   */
  function Password($length = 12)
  {
    $this->length = $length;
     
    $this->configure(true, true, true, false, false);
  }
 
  /**
   * 配置
   */
  function configure($uppercase = false, $lowercase = false, $number = false,
            $special = false, $extra = false
  ) {
    $this->chars = array();
 
    $this->upper_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"
                );
    $this->lower_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"
                );
    $this->number_chars = array(
                 "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"
                );
    $this->special_chars = array(
                 "!", "@", "#", "$", "%", "^", "&", "*", "(", ")"
                );
    $this->extra_chars  = array(
                 "[", "]", "{", "}", "-", "_", "+", "=", "<",
                 ">", "?", "/", "`", "~", "|", ",", ".", ";", ":"
                );
 
    if (($this->uppercase = $uppercase) === true) {
      $this->chars = array_merge($this->chars, $this->upper_chars);
    }
    if (($this->lowercase = $lowercase) === true) {
      $this->chars = array_merge($this->chars, $this->lower_chars);
    }
    if (($this->number = $number) === true) {
      $this->chars = array_merge($this->chars, $this->number_chars);
    }
    if (($this->special = $special) === true) {
      $this->chars = array_merge($this->chars, $this->special_chars);
    }
    if (($this->extra = $extra) === true) {
      $this->chars = array_merge($this->chars, $this->extra_chars);
    }
 
    $this->chars = array_unique($this->chars);
  }
   
  /**
   * 從字符列中生成隨機(jī)密碼
   *
   * @return string
   **/
  function generate()
  {
    if (empty($this->chars)) {
      return false;
    }
 
    $hash    = '';
    $totalChars = count($this->chars) - 1;
     
    for ($i = 0; $i < $this->length; $i++) {
      $hash .= $this->chars[$this->random(0, $totalChars)];
    }
 
    return $hash;
  }
 
  /**
   * 生成隨機(jī)數(shù)字
   *
   * @return int
   */
  function random($min = 0, $max = 0)
  {
    $max_random = 4294967295;
 
    $random = uniqid(microtime() . mt_rand(), true);
    $random = sha1(md5($random));
 
    $value = substr($random, 0, 8);
    $value = abs(hexdec($value));
 
    if ($max != 0) {
      $value = $min + ($max - $min + 1) * $value / ($max_random + 1);
    }
 
    return abs(intval($value));
  }
}

調(diào)用:

<?php
 
include_once 'password.class.php';
 
echo $passGen->generate();
 
//FS4yq74e2LeE

到此,相信大家對(duì)“PHP如何生成隨機(jī)密碼”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI