In an attempt to create a random password in PHP, a developer faced challenges with obtaining an array of characters instead of a string. Additionally, the generated password consisted solely of the letter "a."
function randomPassword() { $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; $alphaLength = strlen($alphabet) - 1; $pass = array(); for ($i = 0; $i < 8; $i++) { $n = rand(0, $alphaLength); $pass[] = $alphabet[$n]; } return implode($pass); }
Note: This function utilizes rand() for generating pseudo-random numbers. For cryptographically secure applications, consider using secure pseudorandom number generators like OpenSSL's random_bytes function.
The above is the detailed content of How to Generate a Truly Random String Password in PHP?. For more information, please follow other related articles on the PHP Chinese website!