user_password

user_password($length = 10)

Generate a random alphanumeric password.

File

core/modules/user/user.module, line 281
Enables the user registration and login system.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function user_password($length = 10) {
  // This variable contains the list of allowable characters for the
  // password. Note that the number 0 and the letter 'O' have been
  // removed to avoid confusion between the two. The same is true
  // of 'I', 1, and 'l'.
  $allowable_characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
 
  // Zero-based count of characters in the allowable list:
  $len = strlen($allowable_characters) - 1;
 
  // Declare the password as a blank string.
  $pass = '';
 
  // Loop the number of times specified by $length.
  for ($i = 0; $i < $length; $i++) {
    do {
      // Find a secure random number within the range needed.
      $index = ord(Crypt::randomBytes(1));
    } while ($index > $len);
 
    // Each iteration, pick a random character from the
    // allowable string and append it to the password:
    $pass .= $allowable_characters[$index];
  }
 
  return $pass;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.