Current location - Recipe Complete Network - Complete cookbook of home-style dishes - How does php generate 32-bit random strings?
How does php generate 32-bit random strings?
If the user registers to generate a random password, the user needs to generate a random password when resetting the password. A random password is just a string of fixed-length characters. The following are three basic methods for generating random strings:

Method one

1. generates a random integer of 33–126, such as 35.

2. Convert 35 into corresponding ASCII characters, such as 35 corresponding to #.

3. Repeat the above steps 1 and 2n times to connect into an n-bit password.

This algorithm mainly uses two functions. The mt_rand (int $min, int $max) function is used to generate random integers, where $min-$max is the value range of ASCII code, where 33-126 is taken, and the value range can be adjusted as required, such as the English letters a-z corresponding to 97- 122 in the ASCII code table. Chr (int $ascii) function is used to convert the corresponding integer $ascii into the corresponding characters.

Function? create_password($pw_length? =?

{?

$randpwd? =? "; ?

For what? ($i? =? 0; ? $i? & lt? $ pw _ length? $i++)?

{?

$randpwd? . =? chr(mt_rand(33,? 126)); ?

}?

Return? $ randpwd?

}?

//? Call this function, passing the length parameter $pw_length? =? 6?

Echo? create _ password(6); Method 2

1, preset a string $chars, including a–z, a–z, 0–9, and some special characters.

2. Randomly select a character in the $chars string.

3. Repeat the second step for n times to obtain a password with a length of n. ..

Function? Generate _ Password (? $ length? =? 8? )? {?

//? Password character set, you can add any characters you need?

$chars? =? abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz 0 123456789! @#$%^&; *()-_? []{ } & lt; & gt~`+=,.; :/? |'; ?

$password? =? "; ?

For what? (? $i? =? 0; ? $i? & lt? $ length; ? $i++? )?

{?

//? There are two ways to get characters.

//? The first is to use? substr? Intercept any character in $chars; ?

//? The second is to take a character array? $chars? Any element?

//? $password? . =? substr($chars,? mt_rand(0,? strlen($chars)? –? 1),? 1); ?

$password? . =? $chars[? mt_rand(0,? strlen($chars)? -? 1)? ]; ?

}?

Return? $ password?

} Method 3

1, preset a character array $chars, including a–z, a–z, 0–9, and some special characters.

2. Randomly select the $length element from the array $chars through array_rand ().

3. According to the obtained key name array $keys, take out the character splicing string from the array $chars. The disadvantage of this method is that it will not take the same characters repeatedly.

Function? make_password(? $ length? =? 8? )?

{?

//? Password character set, you can add any characters you need?

$chars? =? Array ('a',? b ',? c ',? d ',? e ',? f ',? g ',? h ',?

Me',? j ',? k ',? l ',' m ',? n ',? Oh,,? p ',? q ',? r ',? s ',?

t ',? u ',? v ',? w ',? x ',? y ',' z ',? A',? b ',? c ',? d ',?

e ',? f ',? g ',? h ',? Me',? j ',? k ',? l ',' M ',? n ',? Oh,,?

p ',? q ',? r ',? s ',? t ',? u ',? v ',? w ',? x ',? y ',' Z ',?

'0′,? ' 1′,? '2′,? ' 3′,? '4′,? ' 5′,? '6′,? ' 7′,? '8′,? ' 9′,? '! ' ,?

'@','#',? '$',? '%',? '^',? & amp',?' *',?' (',?' )',?' -',?' _',?

'[',? ' ]',? '{',? '}',? & lt',? & gt',? '~',? '`',? '+',? '=',? ',',?

'.',? '; ' ,? ':',? '/',? '? ' ,? '|'); ?

//? Are you online? $chars? Help yourself? $ length? Key names of array elements?

$keys? =? ($chars,? $ length); ?

$password? =? "; ?

For ($ me? =? 0; ? $i? & lt? $ length; ? $i++)?

{?

//? Will. $ length? Array elements are concatenated into a string?

$password? . =? $ chars[$ keys[$ I]]; ?

}?

Return? $ password?

}