Summary of functions for generating random passwords in php

Source: Internet
Author: User
Tags character set generator strlen

The following summarizes some examples for your reference.

Example 1

Simplest generation method

The code is as follows: Copy code


Function generatePassword ($ length = 8)
{
$ Chars = array_merge (range (0, 9 ),
Range ('A', 'z '),
Range ('A', 'z '),
Array ('! ',' @ ',' $ ',' % ',' ^ ','&','*'));
Shuffle ($ chars );
$ Password = '';
For ($ I = 0; $ I <8; $ I ++ ){
$ Password. = $ chars [$ I];
    }
Return $ password;
}


 

Example 2

1. Generate a random integer in 33-126, for example, 35,
2. Convert 35 to corresponding ASCII characters, for example, 35 #
3. Repeat steps 1 and 2 n and connect them to a n-bit password.

The code is as follows: Copy code

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

// Call this function to pass the length parameter $ pw_length = 6
Echo create_password (6 );

Instance

The code is as follows: Copy code

<? Php
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;
}
 
?> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Title> php password generator v 4.0 </title>
<Style type = "text/css">
Body {
Font-family: Arial;
Font-size: 10pt;
}
</Style>
</Head>
<Body>
<Span style = "font-weight: bold; font-size: 15pt;"> Password Generator v4.0 by freemouse </span> <br/>
<? Php
 
If (isset ($ _ GET ['password _ length']) {
If (preg_match ("/([0-9] {1, 8})/", $ _ GET ['password _ length']) {
Print ("password generated successfully: <br/>
<Span style = "font-weight: bold"> ". gen_random_password ($ _ GET ['password _ length']). "</span> <br/> n ");
} Else {
Print ("incorrect password length! <Br/> n ");
 }
}
 
Print <end
Specify the length of the generated password for the password: <br/>
<Form action = "{$ _ SERVER ['php _ SELF ']}" method = "get">
<Input type = "text" name = "password_length">
<Input type = "submit" value = "generate">
</Form>
End;
 
?>
</Body>
</Html>

Example 4


1. Preset a string $ chars, including A-z, a-Z, 0-9, and some special characters
2. Random character in $ chars string
3. Repeat step 2 n times to obtain a password with a length of n.

The code is as follows: Copy code

Function generate_password ($ length = 8 ){
// 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;
}


The tested performance is inferior to the following:

1. Preset a character array $ chars, including A-z, a-Z, 0-9, and some special characters
2. Use array_rand () to randomly select $ length elements from the array $ chars.
3. Extract the string from the array $ chars based on the obtained key name array $ keys. The disadvantage of this method is that the same characters are not repeated.

The code is as follows: Copy code

Function make_password ($ length = 8)
{
// Password character set, which can be any character you need
$ 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', '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 ',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9 ', '! ',
'@', '#', '$', '%', '^ ','&','*','(',')','-', '_',
'[', ']', '{', '}', '<', '> ','~ ', ''',' + ',' = ',',',
'.',';',':','/','? ',' | ');

// Random $ length array element key names in $ chars
$ Keys = array_rand ($ chars, $ length );

$ Password = '';
For ($ I = 0; $ I <$ length; $ I ++)
    {
// Concatenate $ length array elements into strings
$ Password. = $ chars [$ keys [$ I];
    }

Return $ password;
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.