To answer this question specifically, two problems:
Here's a code snippet with the corrections:
Output the random string with the call below:
steadweb 13.8k3 gold badges28 silver badges41 bronze badges answered Dec 4, 2010 at 22:57 Stephen WatkinsStephen Watkins 24.4k14 gold badges65 silver badges98 bronze badges 16
One more way.UPDATED (now this generates any length of string):
That's it. :) answered Nov 3, 2012 at 20:04 A. CheshirovA. Cheshirov 4,6461 gold badge12 silver badges13 bronze badges 20 There are a lot of answers to this question, but none of them leverage a Cryptographically Secure Pseudo-Random Number Generator (CSPRNG). The simple, secure, and correct answer is to use RandomLib and don't reinvent the wheel. For those of you who insist on inventing your own solution, PHP 7.0.0 will provide Safely generating random integers in PHP isn't a trivial task. You should always check with your resident StackExchange cryptography experts before you deploy a home-grown algorithm in production. With a secure integer generator in place, generating a random string with a CSPRNG is a walk in the park. Creating a Secure, Random String
Usage:
Demo: https://3v4l.org/IMJGF (Ignore the PHP 5 failures; it needs random_compat) answered Jun 29, 2015 at 3:41 Scott ArciszewskiScott Arciszewski 32.6k16 gold badges87 silver badges204 bronze badges 15 This creates a 20 character long hexadecimal string:
In PHP 7 (random_bytes()):
answered Sep 4, 2014 at 18:25 8 @tasmaniski: your answer worked for me. I had the same problem, and I would suggest it for those who are ever looking for the same answer. Here it is from @tasmaniski:
Here is a youtube video showing us how to create a random number answered Feb 10, 2013 at 8:24 HumphreyHumphrey 2,4933 gold badges27 silver badges38 bronze badges 6 Depending on your application (I wanted to generate passwords), you could use
Being base64, they may contain
answered Feb 6, 2013 at 17:40 rjmunrorjmunro 26.4k19 gold badges108 silver badges132 bronze badges 6 PHP 7+ Generate cryptographically secure random bytes using random_bytes function.
Possible output
PHP 5.3+ Generate pseudo-random bytes using openssl_random_pseudo_bytes function.
Possible output
The best use case could be
Possible output
answered Jan 25, 2020 at 3:12 Madan SapkotaMadan Sapkota 23.8k11 gold badges112 silver badges115 bronze badges 7 Here is a simple one-liner that generates a true random string without any script level looping or use of OpenSSL libraries.
To break it down so the parameters are clear
This method works by randomly repeating the character list, then shuffles the combined string, and returns the number of characters specified. You can further randomize this, by randomizing the length of the returned string, replacing answered Apr 19, 2014 at 21:18 Kraang PrimeKraang Prime 9,5918 gold badges56 silver badges120 bronze badges 10 A better way to implement this function is:
answered Sep 24, 2012 at 18:08 1
Tada! answered Dec 26, 2012 at 16:32 DavorDavor 1,38716 silver badges32 bronze badges 8
Or just directly echo the return value:
Also, in your function you have a little mistake. Within the for loop, you need to use
answered Dec 4, 2010 at 22:59 BoltClockBoltClock 674k155 gold badges1361 silver badges1333 bronze badges First, you define the alphabet you want to use:
Then, use
Finally, you use this random data to create the password. Because each character in
Alternatively, and generally better, is to use RandomLib and SecurityLib:
2 I've tested performance of most popular functions there, the time which is needed to generate 1'000'000 strings of 32 symbols on my box is:
Please note it is not important how long it really was but which is slower and which one is faster so you can select according to your requirements including cryptography-readiness etc. substr() around MD5 was added for sake of accuracy if you need string which is shorter than 32 symbols. For sake of answer: the string was not concatenated but overwritten and result of the function was not stored. answered Oct 24, 2017 at 12:09 PutnikPutnik 4,9695 gold badges35 silver badges54 bronze badges 1 Here's my simple one line solution to generate a use friendly random password, excluding the characters that lookalike such as "1" and "l", "O" and "0", etc... here it is 5 characters but you can easily change it of course:
answered Oct 4, 2019 at 14:03 rAthusrAthus 7827 silver badges14 bronze badges 7 Short Methods..Here are some shortest method to generate the random string
answered Feb 7, 2017 at 6:12 Punit GajjarPunit Gajjar 4,5137 gold badges32 silver badges64 bronze badges One very quick way is to do something like:
This will generate a random string with the length of 10 chars. Of course, some might say it's a bit more heavy on the computation side, but nowadays
processors are optimized to run md5 or sha256 algorithm very quickly. And of course, if the BassMHL 7,4839 gold badges48 silver badges62 bronze badges answered Oct 24, 2013 at 16:04 AkatoshAkatosh 4289 silver badges17 bronze badges 0
answered Jan 3 at 17:13 Mr. CoderxMr. Coderx 4416 silver badges4 bronze badges Helper function from Laravel 5 framework
answered Feb 17, 2015 at 19:03 artnikproartnikpro 5,1064 gold badges35 silver badges38 bronze badges 1
answered Nov 13, 2012 at 14:45 1 This one was taken from adminer sources:
Adminer, database management tool written in PHP. answered Aug 2, 2016 at 3:15 userlonduserlond 3,4842 gold badges32 silver badges50 bronze badges from the yii2 framework
sxn 1571 silver badge7 bronze badges answered Oct 7, 2020 at 9:45 SXNSXN 891 silver badge3 bronze badges 2
Source from http://www.xeweb.net/2011/02/11/generate-a-random-string-a-z-0-9-in-php/ mike_t 2,3822 gold badges19 silver badges36 bronze badges answered Dec 23, 2017 at 10:57 sxnsxn 1571 silver badge7 bronze badges Another one-liner, which generates a random string of 10 characters with letters and numbers. It will create an array with
Note: this only works in PHP 5.3 and later answered Aug 1, 2014 at 14:20 kasimirkasimir 1,4681 gold badge20 silver badges24 bronze badges 3 One liner. It is fast for huge strings with some uniqueness.
answered May 19, 2015 at 20:47 2
answered Jan 20, 2017 at 16:35 Anjith K PAnjith K P 2,14826 silver badges35 bronze badges Here is how I am doing it to get a true unique random key:
You can use time() since it is a Unix timestamp and is always unique compared to other random mentioned above. You can then generate the md5sum of that and take the desired length you need from the generated MD5 string. In this case I am using 10 characters, and I could use a longer string if I would want to make it more unique. I hope this helps. answered Apr 9, 2013 at 23:16 sherpasherpa 731 silver badge2 bronze badges 4 The edited version of the function works fine, but there is just one issue I found: You used the wrong character to enclose $characters, so the ’ character is sometimes part of the random string that is generated. To fix this, change:
to:
This way only the enclosed characters are used, and the ’ character will never be a part of the random string that is generated. answered Aug 8, 2012 at 16:18 bmcsweebmcswee 1072 silver badges6 bronze badges
and use:
answered Mar 8, 2021 at 10:36 MRMPMRMP 1831 silver badge5 bronze badges I liked the last comment which used openssl_random_pseudo_bytes, but it wasn't a solution for me as I still had to remove the characters I didn't want, and I wasn't able to get a set length string. Here is my solution...
answered Feb 10, 2013 at 21:02 |