Here's the function I use:
function makeRandomString($max=6) {
$i = 0; //Reset the counter.
$possible_keys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$keys_length = strlen($possible_keys);
$str = ""; //Let's declare the string, to add later.
while($i<$max) {
$rand = mt_rand(1,$keys_length-1);
$str.= $possible_keys[$rand];
$i++;
}
return $str;
}
EDIT: As Bill the Lizard said, you'd better add some kind of counter. Although very unlikely, it is possible that the same string can be created twice.