function user_password

Same name and namespace in other branches
  1. 7.x modules/user/user.module \user_password()
  2. 9 core/modules/user/user.module \user_password()

Generate a random alphanumeric password.

Parameters

int $length: The desired password length, in characters.

Return value

string The generated random password.

8 calls to user_password()
DbLogTest::doUser in core/modules/dblog/tests/src/Functional/DbLogTest.php
Generates and then verifies some user events.
EntityReferenceSelectionAccessTest::testUserHandler in core/modules/system/tests/src/Functional/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php
Test the user-specific overrides of the entity handler.
RegisterForm::submitForm in core/modules/user/src/RegisterForm.php
UserCreationTrait::createUser in core/modules/user/tests/src/Traits/UserCreationTrait.php
Create a user with a given set of permissions.
UserPasswordResetTest::testUserPasswordReset in core/modules/user/tests/src/Functional/UserPasswordResetTest.php
Tests password reset functionality.

... See full list

File

core/modules/user/user.module, line 321

Code

function user_password($length = 10) {
  // This variable contains the list of allowed 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'.
  $allowed_characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
  // The maximum integer we want from random_int().
  $max = strlen($allowed_characters) - 1;
  $pass = '';
  for ($i = 0; $i < $length; $i++) {
    $pass .= $allowed_characters[random_int(0, $max)];
  }
  return $pass;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.