搜尋
首頁後端開發php教程php 密碼生成類

php 密碼生成類

Feb 18, 2017 am 09:12 AM

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(
                            &#39;letter&#39; => 1,
                            &#39;number&#39; => 1,
                            &#39;special&#39; => 1
                       );

    private $_length = 8;                 // 密码长度
    private $_num = 1;                    // 密码数量
    private $_special = &#39;!@#$%^&*()_+=-&#39;; //允许的特殊字符


    /** 初始化
    * @param int    $length  密码长度
    * @param int    $num     密码数量
    * @param Array  $rule    密码规则
    * @param String $special 允许的特殊字符
    */
    public function __construct($length=8, $num=1, $rule=array(), $special=&#39;&#39;){

        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!=&#39;&#39;){ // 特殊字符
            $this->_special = $special;
        }

        if($rule){ // 规则

            $t_rule = array();

            if(isset($rule[&#39;letter&#39;]) && in_array($rule[&#39;letter&#39;], array(1,2,3,4,5))){ // 1:可选用 2:必须 3:必须小写 4:必须大写 5:大小写都必须
                $t_rule[&#39;letter&#39;] = $rule[&#39;letter&#39;];
            }

            if(isset($rule[&#39;number&#39;]) && in_array($rule[&#39;number&#39;], array(1,2))){ // 1:可选用 2:必须
                $t_rule[&#39;number&#39;] = $rule[&#39;number&#39;];
            }

            if(isset($rule[&#39;special&#39;]) && in_array($rule[&#39;special&#39;], array(1,2))){ // 1:可选用 2:必须
                $t_rule[&#39;special&#39;] = $rule[&#39;special&#39;];
            }

            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
    */
    private function generate(){

        $password = &#39;&#39;;
        $pool = &#39;&#39;;
        $force_pool = &#39;&#39;;

        if(isset($this->_rule[&#39;letter&#39;])){

            $letter = $this->getLetter();

            switch($this->_rule[&#39;letter&#39;]){
                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[&#39;number&#39;])){

            $number = $this->getNumber();

            switch($this->_rule[&#39;number&#39;]){
                case 2:
                    $force_pool .= substr($number, mt_rand(0,strlen($number)-1), 1);
                    break;
            }

            $pool .= $number;

        }

        if(isset($this->_rule[&#39;special&#39;])){

            $special = $this->getSpecial();

            switch($this->_rule[&#39;special&#39;]){
                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 = &#39;AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz&#39;;
        return $letter;
    }


    /** 数字 */
    private function getNumber(){
        $number = &#39;1234567890&#39;;
        return $number;
    }


    /** 特殊字符 */
    private function getSpecial(){
        $special = $this->_special;
        return $special;
    }

} // class end

?>


<?php
require &#39;GeneratePassword.class.php&#39;;

$rule = array(
    &#39;letter&#39; => 5, // 必须含有大小写字母
    &#39;number&#39; => 2, // 必须含有数字
    &#39;special&#39; => 2 // 必须含有特殊字符
);

$special = &#39;!@#$%_-&#39;;

$obj = new GeneratePassword(8, 10, $rule, $special);
$passwords = $obj->batchGenerate();

echo implode(&#39;<br>&#39;, $passwords);
?>



 以上就是php 密碼產生類別的內容,更多相關內容請關注PHP中文網(www.php.cn)!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何使PHP應用程序更快如何使PHP應用程序更快May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

PHP性能優化清單:立即提高速度PHP性能優化清單:立即提高速度May 12, 2025 am 12:07 AM

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

PHP依賴注入:提高代碼可檢驗性PHP依賴注入:提高代碼可檢驗性May 12, 2025 am 12:03 AM

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

PHP性能優化:數據庫查詢優化PHP性能優化:數據庫查詢優化May 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

簡單指南:帶有PHP腳本的電子郵件發送簡單指南:帶有PHP腳本的電子郵件發送May 12, 2025 am 12:02 AM

phpisusedforsenderemailsduetoitsbuilt-inmail()函數andsupportivelibrariesLikePhpMailerAndSwiftMailer.1)usethemail()functionForbasiceMails,butithasimails.2)butithasimail.2)

PHP性能:識別和修復瓶頸PHP性能:識別和修復瓶頸May 11, 2025 am 12:13 AM

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显著提升PHP应用的性能。

PHP的依賴注入:快速摘要PHP的依賴注入:快速摘要May 11, 2025 am 12:09 AM

依賴性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增強量強制性,可驗證性和MATIALWINABIOS.ItallowSpasspassingDepentenciesLikEdenciesLikedAbaseConnectionStoclasseconnectionStoclasseSasasasasareTers,interitationAseTestingEaseTestingEaseTestingEaseTestingEasingAndScalability。

提高PHP性能:緩存策略和技術提高PHP性能:緩存策略和技術May 11, 2025 am 12:08 AM

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具