PHP Password Generation class instance, PHP build Instance _php tutorial

Source: Internet
Author: User

PHP Password Generation class instance, PHP build instance


This paper introduces the password generation class of PHP implementation and its application method, and shares it for everyone's reference. The specific analysis is as follows:

One, 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.

Second, usage:

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

<?php/** Generate Password class, generated according to specified rules Password * date:2013-12-23 * author:fdipzone * ver:1.0 * * Func: * Public batchgenerate bulk Generate password * Private generate generate a single password * Private getletter get the letter * Private GetNumber get the number * Priva  Te 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 = '!@#$%^&* () _+=-'; Special characters allowed/** initialize * @param int $length password length * @param int $num number of passwords * @param Array $rule Password rules * @param stri Ng $special the special characters allowed */Public function __construct ($length =8, $num =1, $rule =array (), $special = ") {if (Isset ($leng TH) && is_numeric ($length) && $length >=4 && $length <=50) {//length $this->_length = $l     Ength; } if (Isset ($num) && is_numeric ($num) && $num >0 &AMP;&Amp     $num <=100) {//Quantity $this->_num = $num; } if (Isset ($special) && is_string ($special) && $special! = ") {//special characters $this->_special = $spe     cial;        } 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[' Numbe       R '] = $rule [' number ']; } if (Isset ($rule [' Special ']) && in_array ($rule [' Special '], array)) {//1: Optional 2: Must $t _rule[' SPE       cial '] = $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,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 ($special, 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 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?>

The Demo sample program is as follows:

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

The full source of this article can click here to download the site.

It is believed that this article has certain reference value for the C # program design of everybody.


PHP generates random passwords

You can put capital letters and numbers in an array, and then randomly take them.

You can judge that with your original idea.
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. = $a;
}
}

PHP program for batch generation of card number passwords

give you some code, specifically how to change, you own according to your needs to change.
!--? php
function Makecard ()
{
Set_time_limit (0);

//Processing buffer
Ob_end_clean ();
Ob_implicit_flush (true);
Echo Str_pad ("", n);

if (intval ($_post[' num ']>0)) $num =intval ($_post[' num '));//Quantity
if (intval ($_post[' point ']>0)) $point = Intval ($_post[' point '); Number of
if (intval ($_post[' batch ']>0)) $batch =intval ($_post[' batch ')),//Lot number
if (($_post[' ym ']!= ")) $ym =$_ post[' ym '; Release date
Else $ym =date (' ym ');

if ($num ==0) return;

$num = $num *10000;//The number of cards, that is, the number of records

Echo "

Start. Date ("H:i:s"). " ";

for ($i =1; $i <= $num; $i + +)
{
$sn =sprintf ("%02s%s%06s", $batch, $ym, $i);
$seek =mt_rand (0,9999). Mt_rand (0,9999). Mt_rand (0,9999); 12-bit
$start =mt_rand (0,20),
$str =strtoupper (substr (MD5 ($seek), $start, N)),
$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);
//duplicate checking
//Add the code to insert the data here.

echo "End". Date ("H:i:s"). "";
printf ("
successfully generated: password </p> for%s million%s points, $num/1e4, $point);
return $num;
}//function end

$numbers = Makecark (...) Remaining full text >>
 

http://www.bkjia.com/PHPjc/883683.html www.bkjia.com true http://www.bkjia.com/PHPjc/883683.html techarticle PHP Password Generation class instance, PHP Generation Example This article describes the PHP implementation of the password generation class and its application methods, share to everyone for your reference. The specific analysis is as follows: First, php password ...

  • Related Article

    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.