PHP Password Generation class

Source: Internet
Author: User
php Password Generation class

Function:

1. Can set the password length.

2. Can set the number of passwords to generate, batch generation.

3. You can specify password rules, letters, numbers, special characters, etc.


GeneratePassword.class.php


<?php/** Generate Password class, generates password* date:2013-12-23* author:fdipzone* ver:1.0** func:* according to specified rules Public batchgenerate bulk Generate password * Private generate generate a single password * Private getletter get the letter * Private GetNumber Get number                            Word * Private getspecial get special characters */class generatepassword{//class start//password rules default private $_rule = Array ( ' Letter ' = 1, ' number ' = 1, ' Speci    Al ' = 1);                 Private $_length = 8;                    Password length private $_num = 1; Number of passwords private $_special = '!@#$%^&* () _+=-';    Allowed special characters/** initialize * @param int $length password length * @param int $num number of passwords * @param Array $rule Password rules        * @param String $special allowed special characters */Public function __construct ($length =8, $num =1, $rule =array (), $special = ')} { if (Isset ($length) && is_numeric ($length) && $length >=4 && $length <=50) {//length $this->_length = $length; } if (Isset ($num) && is_numeric ($num) && $num >0 && $num <=100) {//Quantity $this-        >_num = $num;  } if (Isset ($special) && is_string ($special) && $special! = ") {//special characters $this->_special        = $special;            } if ($rule) {//rule $t _rule = array ();                if (Isset ($rule [' letter]) && In_array ($rule [' letter '], array (1,2,3,4,5))) {//1: Optional 2: Must 3: Must lowercase 4: Must be uppercase 5: case must be            $t _rule[' letter ') = $rule [' Letter ']; } if (Isset ($rule [' number ']) && in_array ($rule [' number '], array)) {//1: Optional 2: Must $t _            rule[' number '] = $rule [' number ']; } if (Isset ($rule [' Special ']) && in_array ($rule [' Special '], array ()) {//1: Optional 2: Must be $            t_rule[' special '] = $rule [' Special ']; } if ($t _rule) {$this->_rUle = $t _rule;        }}}/** bulk generate password * @return Array */Public function batchgenerate () {$passwords = Array ();        for ($i =0; $i < $this->_num; $i + +) {Array_push ($passwords, $this->generate ());    } return $passwords;        }/** generates a single password * @return String */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,strl                    En ($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,strl                    En ($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,st                    Rlen ($special)-1), 1);            Break } $pool . = $special; } $pool = Str_shuffle ($pool); Randomly disturbed $password = Str_shuffle ($force _pool. substr ($pool, 0, $this->_length-strlen ($force _pool));    Again randomly disrupts return $password;        }/** Letter */Private Function Getletter () {$letter = ' aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz ';    return $letter;        }/** number */Private Function GetNumber () {$number = ' 1234567890 ';    return $number;        }/** Special character */Private function getspecial () {$special = $this->_special;    return $special; }}//Class end?>


Demo


<?phprequire ' GeneratePassword.class.php '; $rule = array (    ' letter ' = = 5,//must contain uppercase and lowercase    ' number ' = 2,//Required shall contain the number    ' special ' and 2//must contain special characters); $special = '!@#$%_-'; $obj = new Generatepassword (8, $rule, $special); $passwor ds = $obj->batchgenerate (); Echo implode (' <br> ', $passwords);? >


The above is the PHP password generation class content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.