This article is mainly to share with you the PHP implementation generated random code, small partners to write a 5-bit random code generation function, requirements: Can contain numbers, letters case, the code as short as possible.
Thinking of solving problems
(1). First Thoughts
Are all impressions of the matching ASCII code for random Read implementation method, here can provide a method as a reference:
function Get_rand_ascii ($PW _length = 5) { $randpwd = '; for ($i = 0; $i < $PW _length; $i + +) { $randpwd. = Chr (Mt_rand); } return $randpwd; }
(2). Second thought
1. Define a random array containing all the characters that can be used
function getrandomstring ($length =5) { //can add any required characters $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 " ); $charsLen = count ($chars)-1; Shuffle ($chars); The array is scrambled $output = ""; for ($i = 0; $i < $length; $i + +) { $output. = $chars [Mt_rand (0, $charsLen)]; } return $output;}
-
Tip: This class of code suggests removing characters that may cause ambiguity, such as: 1, I, L, 0, O, O, 9, Q.
Cons: 5 characters without duplicates
2. Compare the form of the array above 1, the design string as follows:
function Get_pass ($length = 8) { //password character set, you can add any character you need $chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&* ()-_ []{}<>~ ' +=,.;:/?| '; $password = "; for ($i = 0; $i < $length; $i + +) {/ /provides two ways to get a character here///The first is to use SUBSTR to intercept any one 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; }
(3). Third idea
Discover new ideas that can be obtained using cryptographic algorithms
Public Function Getrand () { $rand = rand (10000,100000); $str = substr (Base64_encode ($rand), 0, 5); return $str; }
Expand extension
-
For PHP design, the general random code can be used for verification code, share code generation, pay attention to the limit of the number of bits, if necessary, to ensure the uniqueness of the random code.
-
Use of Password_hash (php>5.5)
Can be used for the user's password-encrypted storage, but one thing to note: If the database is migrated, it will be difficult for Java or. NET languages to apply.
$password = ' password1232456 ';//Front end gets the original password //database stored using the BCRYPT algorithm encrypted password //Here is only for testing, the actual application should be obtained from the data table query //$db _ pass = ' $2y$10$2vjjc.rb/swauntfc9b94.l/ix75kizhvozfpu0dd8uzp07ywlj4q '; $db _pass = Password_hash ($password, password_bcrypt); if (Password_verify ($password, $db _pass)) { echo "password Match"; } else{ echo "password error"; }