Custom functions generated by php random passwords-PHP source code

Source: Internet
Author: User
If you want to achieve random security passwords and user names, I have a friend who serves as the server login password and user name. The phpmyadmin function is used to generate a random password. It is better to use phpmyadmin, the following describes some user-defined functions generated by php random passwords for your reference. If you want to achieve random security passwords and user names, I have a friend who serves as the server login password and user name. The phpmyadmin function is used to generate a random password. It is better to use phpmyadmin, the following describes some user-defined functions generated by php random passwords for your reference.

Script ec (2); script

The length of the generated string can be specified.

Function rand_str ($ length, $ max = FALSE)
{
If (is_int ($ max) & $ max> $ length)
{
$ Length = mt_rand ($ length, $ max );
}
$ Output = '';

For ($ I = 0; $ I <$ length; $ I ++)
{
$ Which = mt_rand (0, 2 );

If ($ which = 0)
{
$ Output. = mt_rand (0, 9 );
}
Elseif ($ which = 1)
{
$ Output. = chr (mt_rand (65,90 ));
}
Else
{
$ Output. = chr (mt_rand (97,122 ));
}
}
Return $ output;
}
Call instance:
$ Randstr = rand_str (16 );

Function for generating random strings

/**
* Generate random strings
*
* Generates a random string of the specified length and returns it to the user.
*
* @ Access public
* @ Param int $ len: number of digits of the string generated
* @ Return string
*/
Function randStr ($ len = 6 ){
$ Chars = 'abdefghjklmnpqrstvwxyabdefghijkmnpqrstvwxy23456789 # % * '; // characters to build the password from
Mt_srand (double) microtime () * 1000000 * getmypid (); // seed the random number generater (must be done)
$ Password = '';
While (strlen ($ password) <$ len)
$ Password. = substr ($ chars, (mt_rand () % strlen ($ chars), 1 );
Return $ password;
}
?>


Create a character pool.


Function randomkeys ($ length)
{
$ Pattern = '1234567890abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLOMNOPQRSTUVWXYZ,./& l
T;> ?; #:@~ [] {}-_ = +) (* & ^ % $ ?! '; // Character pool
For ($ I = 0; $ I <$ length; $ I ++)
{
$ Key. = $ pattern {mt_rand ()}; // generate a php Random Number
}
Return $ key;
}
Echo randomkeys (8 );


No need to create a character pool

Function randomkeys ($ length)
{
$ Output = '';
For ($ a = 0; $ a <$ length; $ a ++ ){
$ Output. = chr (mt_rand (35,126); // generate a php Random Number
}
Return $ output;
}
Echo randomkeys (8 );


Examples of random usernames and passwords

// Randomly generate a user name (Length: 6-13)

Function create_password ($ pw_length = 4 ){
$ Randpwd = '';
For ($ I = 0; $ I <$ pw_length; $ I ++ ){
$ Randpwd. = chr (mt_rand (33,126 ));
}
Return $ randpwd;
}

Function generate_username ($ length = 6 ){
// Password character set, which can be any character you need
$ Chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789! @ # $ % ^ & * ()-_ [] {}<> ~ '+ = ,.;:/? | ';
$ Password = '';
For ($ I = 0; $ I <$ length; $ I ++)
{
// Two character acquisition methods are provided here
// Use substr to intercept any character in $ chars;
// The second method is to take any element of the character array $ chars.
// $ Password. = substr ($ chars, mt_rand (0, strlen ($ chars)-1), 1 );
$ Password. = $ chars [mt_rand (0, strlen ($ chars)-1)];
}
Return $ password;
}

// Call
$ UserId = 'user'. generate_username (6 );
$ Pwd = create_password (9 );

Mt_srand generates random seeds. The password length can be defined at will, and the maximum length is 32 characters.

Mt_srand (double) microtime () * 1000000 );

Function gen_random_password ($ password_length = 32, $ generated_password = ""){
$ Valid_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
$ Chars_length = strlen ($ valid_characters)-1;
For ($ I = $ password_length; $ I --;){
// $ Generated_password. = $ valid_characters [mt_rand (0, $ chars_length)];

$ Generated_password. = substr ($ valid_characters, (mt_rand () % (strlen ($ valid_characters), 1 );
}
Return $ generated_password;
}
?>



Php Password generator



Password generator



If (isset ($ _ GET ['password _ length']) {
If (preg_match ("/([0-9] {1, 8})/", $ _ GET ['password _ length']) {
Print ("password generated successfully:

". Gen_random_password ($ _ GET ['password _ length'])."

N ");
} Else {
Print ("Incorrect password length!

N ");
}
}

Print <end
Specify the length of the generated password for the password:



End;
?>

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.