PHP Password Generation class

Source: Internet
Author: User
Tags lowercase strlen

PHP Password Generation class

Function:

1. can set password length.

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

3. Can specify the rules of the password, letters, numbers, special characters and so on.

GeneratePassword.class.php

<?php/** Generate Password class, generated according to the specified rule Password * date:2013-12-23 * author:fdipzone * ver:1.0 * * Func: * Public batchgenerate Batch generation password * Private generate generate a single password * Private getletter get the letter * pri Vate GetNumber Get Digital * Private getspecial get special characters */class generatepassword{//Class Start/  
                            /password rule default private $_rule = Array (' letter ' => 1,  
      
    ' Number ' => 1, ' Special ' => 1);                 Private $_length = 8;                    Password length private $_num = 1; Number of passwords private $_special = '!@#$%^&* () _+=-'; Allowed special characters/** * @param int $length password length * @param int $num number of passwords * @param Array $rule Password rule * @param String $special special characters allowed */Public function __construct ($length =8, $num =1, $rul E=array (), $sPecial= ') {if (Isset ($length) && is_numeric ($length) && $length >=4 && $length &  
        LT;=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 ($rule [' letter ']) && in_array ($rule [' letters '], array (1,2,3,4,5)) {//1: Optional 2: Must 3: Must lowercase 4: Must uppercase 5: case must be isset  
            $t _rule[' letter '] = $rule [' Letter '];  
                if (Isset ($rule [' number ']) && in_array ($rule [' number '], array (1,2)) {//1: Optional 2: Must be  
            $t _rule[' number '] = $rule [' number '];  
      
          }  if (Isset ($rule [' Special ']) && in_array ($rule [' Special '], array (1,2))) {//1: Optional 2: Must $t _rule['  
            Special '] = $rule [' Special '];  
            } if ($t _rule) {$this->_rule = $t _rule; /** Batch Generation Password * @return Array */Public function Batchg  
      
        Enerate () {$passwords = array ();  
        For ($i =0 $i < $this->_num; $i + +) {Array_push ($passwords, $this->generate ());  
    return $passwords; /** generate a single password * @return String See more highlights in this column: Http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PH  
        p/* * 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 ($s  
                    Pecial, Mt_rand (0,strlen ($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 upset 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

<?php  
require ' GeneratePassword.class.php ';  
      
$rule = Array (  
    ' letter ' => 5,///must contain the uppercase  
    and lowercase ' number ' => 2,///must contain the digital  
    ' special ' => 2///must contain special characters  
);  
      
$special = '!@#$%_-';  
      
$obj = new Generatepassword (8, $rule, $special);  
$passwords = $obj->batchgenerate ();  
      
echo implode (' <br> ', $passwords);  
? >

SOURCE Download Address: http://download.csdn.net/detail/fdipzone/6760311

Author: csdn blog proud Snow star Maple

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.