...

View Full Version : making random string



mivec
02-19-2004, 05:36 PM
to all,
is there an in built function in php that cna generate a random string? example:

rand() are for generating numbers right?

so what is the function for generating a string?what i mean of string is like probably---> fsr45mdk43 or something like that.

pls help..thanking u in advance.

missing-score
02-19-2004, 05:48 PM
I used to have quite a nice way of making a random string but I have forgotten it. I think it used rand though.

This is how invisionboard generate a random password



function make_password()
{
$pass = "";
$chars = array(
"1","2","3","4","5","6","7","8","9","0",
"a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J",
"k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T",
"u","U","v","V","w","W","x","X","y","Y","z","Z");

$count = count($chars) - 1;

srand((double)microtime()*1000000);

for($i = 0; $i < 8; $i++)
{
$pass .= $chars[rand(0, $count)];
}

return($pass);
}


you could obviously change the length and function name. Also, i think the (double) method is no longer used so, you could replace:




srand((double)microtime()*1000000)

with:

$microtime = microtime()*1000000;
settype($microtime, 'float');
srand($microtime);

eak
02-19-2004, 06:14 PM
this link may help you. ( http://www.phpnoise.com/tutorials/1/2 )
on this site is uses $string = md5(rand(0,9999));
I have tried it out and it works. you just have to use substr($string,start#,end#); to get the desired string length.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum