PHP: Create randomized string or random password
Question by SimplyMe | Last update on 2022-11-19 | Created on 2011-11-25
I want to create a random string using PHP that should be used as an initial password. So, what you usually get after a registration by e-mail or if you request a new password.
Does PHP know a specific function for that, or how do you do best?
Related Topics
The Secure Password
Info | 0 Comments
PHP: Check Strings with Ctype-Functions for Character Classes
Article | 0 Comments
MySQL: Add or Insert Random Number
Tip | 0 Comments
Delphi/Lazarus: Password Edit
Tip | 0 Comments
Create random Words from specific Syllables
Tutorial | 0 Comments
Send Form Input as an Array to PHP Script
Tip | 0 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
The easiest way to achieve this, is as follows with a combination of md5() and rand():
The function rand() creates a random number and md5() makes an MD5 hash from that, which will look similiar to the second line above.
If this should be too long for your purpose, you can also directly limit the string with substr():
If you replace the 10 with a different number, you can shorten the number of characters. In the above example, I once chose a length of 10 characters.
Or we just create a small function directly from it:
Alternatively, you can also use the function uniqid(), which, however, does not provide quite as good results:
This function creates a unique ID out of the current time, so the first places are always the same, if you call the function in quick succession. Of course, if you only want to have any random string, this should be okay.
Limitations of these functions
The advantage of my functions mentioned above is, that it is very easy to implement and the strings can be used as passwords (more about secure passwords). But there is also a disadvantage: the strings are consisting only of lowercase letters and numbers. We have no way to determine which characters are present in the string. If you wish to do that, you have to use your own, even more complicated function.
Last update on 2022-11-19 | Created on 2011-12-15
The examples of Computer Expert are both simple and ingenious. Nevertheless, here's a small example of how you can quickly craft a function for yourself, which can be used to create random strings out of a defined pool of characters:
The whole works as follows: First, we define a string (in this case, it is wraped because of the length) with all characters to appear. Of course, you can still add other characters, who would like to include or you can remove the characters, you do not want to have in the string.
Then, we define our random string with $str and write in $num the number of possible characters. Next, we call as often as our new string should be long, the random number generator rand() in a loop and every time, we choouse the corresponding character of the random number out of the string of possible characters.
Finally, we return the completed string. A possible, generated string might be "G3pgIA".
2011-12-16 at 09:16