PHP random password generation function

Source: Internet
Author: User
PHP generates a random password function, which can be used to generate a random number or generate a random password. if you need a random password, refer to it. the specific function code is as follows: PHP generates random number password function (six by default) functionrandStr ($ len6, $ formatALL) {switch ($ format) {caseALL: $ charsABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop PHP generates random password function, this function can be used to generate a random number or a random password. For more information, see function code:

// PHP random number generation and password function (six digits by default)

function randStr($len=6,$format='ALL'){
switch($format){
case 'ALL':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
break;
case 'CHAR':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~';
break;
case 'NUMBER':
$chars='0123456789';
break;
default :
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
break;
}
mt_srand((double)microtime()*1000000*getmypid());
$password='';
while(strlen($password)<$len){
$password.=substr($chars,(mt_rand()%strlen($chars)),1);
}
return $password;

}

Several methods for generating random numbers in php are summarized as follows:

The first method is very simple. you can directly use the built-in functions:

Srand (double) microtime () * 1000000 );
// Randomly generate an integer between 0 and 99
$ Randval = rand (0,99999999 );
Echo $ randval ,'
';


/* Similar output: 32659912 */

The second method is a little more complex. it not only generates random strings with only numbers, but also contains various special characters:

function randomkeys($length){
$output='';
for($a=0;$a<$length; $a++){
$output.=chr(mt_rand(33, 126));
}
return $output;
}
echo randomkeys(20);


/* Similar output: EGztLufY \ Eq/br {y!;> P */

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.