简体   繁体   中英

I'm having trouble creating a random string

I have a little script that is used to generate a random string of 25 characters. It works about 70% of the time and other times it creates strings that are only 10.

$unique = substr(md5(microtime()),rand(0,26),25);

My goal is to have a random string of (lower case) letters and numbers without having to create a function

This is becouse you are cutting it to a random length with rand(0, 26).

Try this instead:

$unique = substr(md5(microtime()), 0, 25);

Don't use md5(microtime()). You might think it's more secure than md5(rand()), but it isn't.

With a decent amount of tries and a method of syncing (like a clock on your website) one can predict the result of microtime() to the millisecond.

use md5(rand())

or if you want it to be more secure use md5(microtime().rand())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM