w3resource

PHP String Exercises : Generate simple random password from a specified string


9. Generate Random Password Without rand()

Write a PHP script to generate simple random password [do not use rand() function] from a given string.

Sample string: '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz'
Note : Password length may be 6, 7, 8 etc.

Sample Solution:

PHP Code:

<?php
// Define a function named password_generate that takes the number of characters as input
function password_generate($chars) 
{
  // Define a string containing all possible characters for the password
  $data = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz';
  // Shuffle the characters in the string and extract a substring of length $chars
  return substr(str_shuffle($data), 0, $chars);
}
// Call the password_generate function with 7 characters and echo the generated password
echo password_generate(7)."\n";
?>

Output:

89Qp3AK

Explanation:

In the exercise above,

  • <?php: This tag indicates the beginning of a PHP script.
  • Function password_generate($chars): This line defines a PHP function named "password_generate()" that takes one parameter '$chars', which represents the number of characters in the password to be generated.
  • {: Opening curly brace indicates the beginning of the function body.
  • $data = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz';: This line creates a string named '$data' containing a pool of characters from which the password will be generated. It includes digits (0-9), uppercase letters (A-Z), and lowercase letters (a-z).
  • return substr(str_shuffle($data), 0, $chars);: This line generates a random password by performing the following steps:
    • str_shuffle($data): Randomly shuffles the characters in the '$data' string.
    • substr(...): Takes a substring of the shuffled characters, starting from index 0 and having a length of '$chars'. This ensures that the generated password has the specified number of characters.
    • return ...;: Returns the generated password from the function.
  • }: Closing curly brace indicates the end of the function body.
  • echo password_generate(7)."\n";: This line calls the "password_generate()" function with an argument of 7, indicating that a password with 7 characters should be generated. It then echoes the generated password followed by a newline character.
  • ?>: This tag indicates the end of the PHP script.

Flowchart :

Flowchart: Generate simple random password from a specified string

For more Practice: Solve these Related Problems:

  • Write a PHP function that generates a random password of a given length using the shuffle() function on a character set string.
  • Write a PHP script to create a secure random password by shuffling a predefined string and then slicing a substring for the password.
  • Write a PHP program to generate multiple random passwords using a non-rand() method, and then output them in a list with varying lengths.
  • Write a PHP function to generate a password by splitting a character pool into an array, shuffling the array, and then joining a subset of characters.

Go to:


PREV : Format as Currency.
NEXT : Replace First Occurrence of "the".

PHP Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.