php密碼產生類

來源:互聯網
上載者:User

php 密碼產生類

功能:

1.可設定密碼長度。

2.可設定要產生的密碼個數,批量產生。

3.可以指定密碼的規則,字母,數字,特殊字元等。

GeneratePassword.class.php

<?php  /** Generate Password class,根據指定規則產生password *   Date:   2013-12-23 *   Author: fdipzone *   Ver:    1.0 * *   Func: *   public  batchGenerate 批量產生密碼 *   private generate      產生單個密碼 *   private getLetter     擷取字母   *   private getNumber     擷取數字 *   private getSpecial    擷取特殊字元 */      class GeneratePassword{ // class start            // 密碼的規則 default      private $_rule = array(                              'letter' => 1,                              'number' => 1,                              'special' => 1                         );            private $_length = 8;                 // 密碼長度      private $_num = 1;                    // 密碼數量      private $_special = '!@#$%^&*()_+=-'; //允許的特殊字元                  /** 初始化     * @param int    $length  密碼長度     * @param int    $num     密碼數量     * @param Array  $rule    密碼規則     * @param String $special 允許的特殊字元     */    public function __construct($length=8, $num=1, $rule=array(), $special=''){                if(isset($length) && is_numeric($length) && $length>=4 && $length<=50){ // 長度              $this->_length = $length;          }                if(isset($num) && is_numeric($num) && $num>0 && $num<=100){ // 數量              $this->_num = $num;          }                if(isset($special) && is_string($special) && $special!=''){ // 特殊字元              $this->_special = $special;          }                if($rule){ // 規則                    $t_rule = array();                    if(isset($rule['letter']) && in_array($rule['letter'], array(1,2,3,4,5))){ // 1:可選用 2:必須 3:必須小寫 4:必須大寫 5:大小寫都必須                  $t_rule['letter'] = $rule['letter'];              }                    if(isset($rule['number']) && in_array($rule['number'], array(1,2))){ // 1:可選用 2:必須                  $t_rule['number'] = $rule['number'];              }                    if(isset($rule['special']) && in_array($rule['special'], array(1,2))){ // 1:可選用 2:必須                  $t_rule['special'] = $rule['special'];              }                    if($t_rule){                  $this->_rule = $t_rule;              }                }            }                  /** 批量產生密碼     * @return Array     */    public function batchGenerate(){                $passwords = array();                for($i=0; $i<$this->_num; $i++){              array_push($passwords, $this->generate());          }                return $passwords;      }                  /** 產生單個密碼     * @return String 查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/    */    private function generate(){                $password = '';          $pool = '';          $force_pool = '';                if(isset($this->_rule['letter'])){                    $letter = $this->getLetter();                    switch($this->_rule['letter']){                  case 2:                      $force_pool .= substr($letter, mt_rand(0,strlen($letter)-1), 1);                      break;                        case 3:                      $force_pool .= strtolower(substr($letter, mt_rand(0,strlen($letter)-1), 1));                      $letter = strtolower($letter);                      break;                        case 4:                      $force_pool .= strtoupper(substr($letter, mt_rand(0,strlen($letter)-1), 1));                      $letter = strtoupper($letter);                      break;                        case 5:                      $force_pool .= strtolower(substr($letter, mt_rand(0,strlen($letter)-1), 1));                      $force_pool .= strtoupper(substr($letter, mt_rand(0,strlen($letter)-1), 1));                      break;              }                    $pool .= $letter;                }                if(isset($this->_rule['number'])){                    $number = $this->getNumber();                    switch($this->_rule['number']){                  case 2:                      $force_pool .= substr($number, mt_rand(0,strlen($number)-1), 1);                      break;              }                    $pool .= $number;                }                if(isset($this->_rule['special'])){                    $special = $this->getSpecial();                    switch($this->_rule['special']){                  case 2:                      $force_pool .= substr($special, mt_rand(0,strlen($special)-1), 1);                      break;              }                    $pool .= $special;          }                $pool = str_shuffle($pool); // 隨機打亂                $password = str_shuffle($force_pool. substr($pool, 0, $this->_length-strlen($force_pool))); // 再次隨機打亂                return $password;            }                  /** 字母 */    private function getLetter(){          $letter = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz';          return $letter;      }                  /** 數字 */    private function getNumber(){          $number = '1234567890';          return $number;      }                  /** 特殊字元 */    private function getSpecial(){          $special = $this->_special;          return $special;      }        } // class end        ?>

demo:

<?php  require 'GeneratePassword.class.php';        $rule = array(      'letter' => 5, // 必須含有大小寫字母      'number' => 2, // 必須含有數字      'special' => 2 // 必須含有特殊字元  );        $special = '!@#$%_-';        $obj = new GeneratePassword(8, 10, $rule, $special);  $passwords = $obj->batchGenerate();        echo implode('<br>', $passwords);  ?>

源碼下載地址:http://download.csdn.net/detail/fdipzone/6760311

作者:csdn部落格 傲雪星楓

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.