PHP generate random password class share _ PHP Tutorial

Source: Internet
Author: User
PHP generates random password class sharing. Class Code: php *** PHP-PasswordGeneratorClass * Version1.0.0 *** if (@! Is_object ($ passGen) |! Isset ($ passGen) {$ passGennewPassword;} classPassword {*** uppercase letter code:

<? Php/*** PHP-Password Generator Class * Version 1.0.0 **/if (@! Is_object ($ passGen) |! Isset ($ passGen) {$ passGen = new Password;} class Password {/*** capital letter A-Z ** @ var array */protected $ uppercase_chars; /*** lowercase letter a-z ** @ var array */protected $ lowercase_chars;/*** Arabic numerals 0-9 ** @ var array */protected $ number_chars; /*** special character ** @ var array */protected $ special_chars;/*** other special characters ** @ var array */protected $ extra_chars; /*** all characters used to generate the password ** @ var array */protected $ Chars = array ();/*** password length ** @ var array */public $ length; /*** whether to use uppercase letters ** @ var boolean */public $ uppercase;/*** whether to use lowercase letters ** @ var boolean */public $ lowercase; /*** use Arabic numerals ** @ var boolean */public $ number;/*** use special characters ** @ var boolean */public $ special; /*** whether to use additional special characters ** @ var boolean */public $ extra; /*** initialize Password setting ** @ param int $ length */function Password ($ length = 12 ){ $ This-> length = $ length; $ this-> configure (true, false, false );} /*** configure */function configure ($ uppercase = false, $ lowercase = false, $ number = false, $ special = false, $ extra = false) {$ this-> chars = array (); $ this-> upper_chars = array ("A", "B", "C", "D", "E ", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O ", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y ", "Z"); $ this-> lower _ Chars = array ("a", "B", "c", "d", "e", "f", "g", "h", "I ", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s ", "t", "u", "v", "w", "x", "y", "z "); $ this-> number_chars = array ("1", "2", "3", "4", "5", "6", "7", "8 ", "9", "0"); $ this-> special_chars = array ("! "," @ "," # "," $ "," % "," ^ ","&","*","(",")"); $ This-> extra_chars = array ("[", "]", "{", "}", "-", "_", "+", "= ", "<", "> ","? ","/","'","~ "," | ",",",". ","; ",": "); If ($ this-> uppercase = $ uppercase) = true) {$ this-> chars = array_merge ($ this-> chars, $ this-> upper_chars);} if ($ this-> lowercase = $ lowercase) = true) {$ this-> chars = array_merge ($ this-> chars, $ this-> lower_chars);} if ($ this-> number = $ number) == true) {$ this-> chars = array_merge ($ this-> chars, $ this-> number_chars);} if ($ this-> special = $ special) = true) {$ This-> chars = array_merge ($ this-> chars, $ this-> special_chars);} if ($ this-> extra = $ extra) === true) {$ this-> chars = array_merge ($ this-> chars, $ this-> extra_chars );} $ this-> chars = array_unique ($ this-> chars);}/*** generate a random password from the character column ** @ return string **/function generate () {if (empty ($ this-> chars) {return false ;}$ hash = ''; $ totalChars = count ($ this-> chars)-1; for ($ I = 0; $ I <$ this-> Length; $ I ++) {$ hash. = $ this-> chars [$ this-> random (0, $ totalChars)];} return $ hash ;} /*** generate a random number ** @ return int */function random ($ min = 0, $ max = 0) {$ max_random = 4294967295; $ random = uniqid (microtime (). mt_rand (), true); $ random = sha1 (md5 ($ random); $ value = substr ($ random, 0, 8 ); $ value = abs (hexdec ($ value); if ($ max! = 0) {$ value = $ min + ($ max-$ min + 1) * $ value/($ max_random + 1 );} return abs (intval ($ value ));}}

Call:

<?php include_once 'password.class.php'; echo $passGen->generate(); //FS4yq74e2LeE

Using php/*** PHP-Password Generator Class * Version 1.0.0 **/if (@! Is_object ($ passGen) |! Isset ($ passGen) {$ passGen = new Password;} class Password {/*** uppercase letter...

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.