Php password generation class instance

Source: Internet
Author: User
This article mainly introduces the php password generation class, which can generate multiple formats of passwords as required. it is a very practical PHP file, for more information about the password generation class implemented by php and its application methods, see the examples in this article. The specific analysis is as follows:

I. php password generation functions:

1. you can set the password length.
2. you can set the number of passwords to be generated and generate them in batches.
3. you can specify password rules, including letters, numbers, and special characters.

II. usage:

The GeneratePassword. class. php class file is as follows:

<? Php/** Generate Password class, Generate password * Date: 2013-12-23 * Author: fdipzone * Ver: 1.0 ** Func: * public batchGenerate generate passwords in batches * private generate single password * private getLetter get letter * private getNumber get number * private getSpecial get special character */class GeneratePassword {// class start // password default private $ _ rule = array ('letter' => 1, 'number' => 1, 'special '=> 1); private $ _ length = 8; // password length Degree private $ _ num = 1; // number of passwords private $ _ special = '! @ # $ % ^ & * () _ + = -'; // Special characters allowed/** initialization * @ param int $ length password length * @ param int $ num password quantity * @ param Array $ rule password rule * @ param String $ special 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 Character $ this-> _ special = $ special;} if ($ rule) {// rule $ t_rule = array (); if (isset ($ rule ['letter ']) & in_array ($ rule ['letter'], array (, 5) {// 1: optional; 2: required; 3: required; lowercase; 4: required; 5: required. $ t_rule ['letter '] = $ rule ['letter'];} if (isset ($ rule ['Number']) & in_array ($ rule ['Number'], array (1, 2) {// 1: optional; 2: $ t_rule ['Number'] = $ rule ['Number'];} if (isset ($ rule ['special ']) & in_array ($ rule ['sp Ecial '], array (1, 2) {// 1: optional; 2: required $ t_rule ['special'] = $ rule ['special '];} if ($ t_rule) {$ this-> _ rule = $ t_rule ;}}/ ** generate passwords in batches * @ return Array */public function batchGenerate () {$ passwords = array (); for ($ I = 0; $ I <$ this-> _ num; $ I ++) {array_push ($ passwords, $ this-> generate ();} return $ passwords;}/** generate 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, 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); $ let Ter = 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); // random disruption $ password = str_shuffle ($ force_pool. substr ($ pool, 0, $ this-> _ length-strlen ($ force_pool); // random disruption of return $ password again;}/** Letter */pr Ivate function getLetter () {$ letter = 'clerk'; return $ letter;}/** number */private function getNumber () {$ number = '20140901 '; return $ number;}/** special character */private function getSpecial () {$ special = $ this-> _ special; return $ special ;}// class end?>

The demo sample program is as follows:

<? Php require 'generatepassword. class. php '; $ rule = array ('letter' => 5, // The upper and lower case letters 'number' => 2, // The number 'special '=> 2 // the number must contain special characters); $ special = '! @ # $ % _-'; $ Obj = new GeneratePassword (8, 10, $ rule, $ special); $ passwords = $ obj-> batchGenerate (); echo implode ('
', $ Passwords);?>

Click here to download the complete source code for this article.

I believe this article has some reference value for your C # program design.

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.