An Example of the Method of Generating Random Codes in PHP

  • 2021-12-05 05:54:33
  • OfStack

In this paper, the idea and method of generating random codes by PHP are described with examples. Share it for your reference, as follows:

Background

Today, because of boredom, my friends let me write a function that generates 5-bit random code. Requirements: it can contain numbers, letters in upper and lower case, and the code is as short as possible.

Thoughts of solving problems

(1). The first idea

They are all the implementation methods of matching ASCII code for random reading in the impression, and one method can be provided here as a reference:


function get_rand_ascii($pw_length = 5){
  $randpwd = '';
  for ($i = 0; $i < $pw_length; $i++) {
    $randpwd .= chr(mt_rand(33, 99));
  }
  return $randpwd;
}

(2). The second idea

1. Define a random array with all the characters you can use


function getRandomString($length=5) {
  // You can add any characters you want 
  $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 ); //  To scramble an array 
  $output = "";
  for($i = 0; $i < $length; $i ++) {
    $output .= $chars [mt_rand ( 0, $charsLen )];
  }
  return $output;
}

Tip: For this kind of code, it is recommended to remove the characters that may cause ambiguity, such as: 1, I, l, 0, o, O, 9, q.

Disadvantages: 5 characters are not repeated

2. Compare the array form in the above 1, and refer to the design string form as follows:


function get_pass( $length = 8 ) {
  //  Password character set, you can add any characters you need 
  $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
  $password = '';
  for ( $i = 0; $i < $length; $i++ ) {
    //  There are two ways to get characters here 
    //  No. 1 1 Species are used  substr  Interception $chars Any of the 1 Bit character; 
    //  No. 1 2 The type is to take the character array  $chars  Any element of 
    // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
  }
  return $password;
}

(3). The third idea

The new ideas discovered can be obtained by using encryption algorithms


public function getRand(){
  $rand = rand(10000,100000);
  $str = substr(base64_encode($rand), 0, 5);
  return $str;
}

Hint: md5() Function has only numbers and lowercase letters

Discover: base64_encode() Can meet the requirements

Extension and extension

For PHP design, 1-like random code can be used to generate verification code and sharing code, paying attention to the limitation of digits, and ensuring the uniqueness of the random code if necessary.

password_hash Use of (PHP) > 5.5)

Can be used for encrypted storage of users' passwords, but one thing to note: If the database is migrated, it will be difficult to apply to java or.net languages.


$password = 'password1232456';// Front end   The original password obtained 
// Database stored   Use BCRYPT Algorithm encrypted password 
// This is for testing only, and should be queried from the data table in practical application 
//$db_pass = '$2y$10$2vJJC.rb/swAUnTfc9B94.l/ix75kiZHvOZFpu0Dd8uzp07YWlj4q';
$db_pass = password_hash($password, PASSWORD_BCRYPT);
if (password_verify($password , $db_pass)){
  echo " Password matching ";
}else{
  echo " Password error ";
}

Reference article:

https://www.ofstack.com/article/71635.htm

PS: Here are two online tools with similar functions for your reference:

Online random number/string generation tool:
http://tools.ofstack.com/aideddesign/suijishu

Online random character/random password generation tool:
http://tools.ofstack.com/aideddesign/rnd_password

For more readers interested in PHP related contents, please check the special topics of this site: "Summary of PHP Mathematical Operation Skills", "Summary of php String (string) Usage", "PHP Data Structure and Algorithm Tutorial", "Summary of php Programming Algorithm", "Encyclopedia of PHP Array (Array) Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: