Php password generation class instance, php generation instance
The example in this article describes the password generation class implemented by php and Its Application Methods, and shares it with you for your reference. 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 ('<br>', $ 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.
PHP generates random passwords
You can place uppercase letters and numbers in an array and then randomly retrieve them.
With your original ideas, you can make judgments in this way.
For ($ I = 0; $ I <$ pw_length; $ I ++)
{
$ A = char (mt_rand (33,126 ));
If ($ a <'0') | ($ a> 'Z') | ($ a> '9 ') & ($ a <'A '))
{
$ I --
}
Else
{
$ Randpwd. = $;
}
}
Php program for batch generation of card numbers and passwords
Let's give you some code. How to change it? You can change it based on your own needs.
<? Php
Function MakeCard ()
{
Set_time_limit (0 );
// Process the buffer
Ob_end_clean ();
Ob_implicit_flush (true );
Echo str_pad ("", 256 );
If (intval ($ _ POST ['num']> 0) $ num = intval ($ _ POST ['num']); // quantity
If (intval ($ _ POST ['point']> 0) $ point = intval ($ _ POST ['point']); // points
If (intval ($ _ POST ['batch']> 0) $ batch = intval ($ _ POST ['batch']); // batch number
If ($ _ POST ['ym']! = "") $ Ym = $ _ POST ['ym']; // date of release
Else $ ym = date ('ymm ');
If ($ num = 0) return;
$ Num = $ num * 10000; // number of cards, that is, the number of records
Echo "<p> Start". date ("H: I: s ")."";
For ($ I = 1; $ I <= $ num; $ I ++)
{
$ Sn = sprintf ("% 02 s % 06 s", $ batch, $ ym, $ I );
$ Seek = mt_rand (). mt_rand (). mt_rand (); // 12 digits
$ Start = mt_rand (0, 20 );
$ Str = strtoupper (substr (md5 ($ seek), $ start, 12 ));
$ Str = str_replace ("O", chr (mt_rand (65,78), $ str );
$ Str = str_replace ("0", chr (mt_rand (65,78), $ str );
$ Row = array ('sn '=> $ sn, 'Password' => $ str, 'created' => time (), 'point' => $ point );
// Re-Query
// Add the data insertion code here.
Echo "end". date ("H: I: s ")."";
Printf ("<br> successfully generated: % s passwords at % s </p>", $ num/1e4, $ point );
Return $ num;
} // Function end
$ Numbers = MakeCark (... the remaining full text>