+5  A: 

The question is not very clear as to what exactly you want. If you want to generate such a string randomly you can do something like:

$length = 20;
$characters = ‘0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ’;
$random_string = "";    
for ($p = 0; $p < $length; $p++) {
    $random_string .= $characters[mt_rand(0, strlen($characters))];
}

To ensure the newly generated token is unique, you'll have to keep track of all the previously generated tokens and perform a check.

codaddict
Need to be more than a random string, need to be unique because it will be used like ID
ElAlecs
Yes, that's what codaddict said in his last sentence...
Boldewyn
...and what is either homework (if you need it to be unique for a single page view) or depends strongly on relative to what you want it be unique. In the latter case, you have to implement the test yourself (database query, file check, whatever).
Boldewyn
The problem using random string and make a validation in a database is after many keys created it comes slow, that's why UUID is a great solution, but is not practice give it long string to a client
ElAlecs
A: 
ElAlecs
A: 

if you want a short unique id you definitely need a database to keep all previous ids.

while (true)
{
    $prefix = "";
    $mid = $prefix . rand(100000,999999) . chr(rand(65, 90));
    $check = mysql_query("SELECT id FROM ids WHERE id = '$mid'");
    if (mysql_num_rows($check) != 0)
    {
        //duplicate
    }
    else
    {
        mysql_query("insert into ids VALUES('$mid')");
        break 1;
    }
}
Ergec
Yeah, I think it too but after some created keys it comes a slow solution. This is why I'm looking for something like UUID but in a short way, maybe the best solution is still using an autoincrement ID, convert it to base36 and complete the string length with a generic character
ElAlecs
You can also use microtime since date never goes back, there is no way to get a duplicate.
Ergec
A: 

i think you answered the question in your last comment, yes, generate UIDs directly from your database autoincrement ids + fill characters, for example

function uid($n) {
    $id_part = base_convert($n, 10, 36);
    $rand_part = str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789');
    return sprintf("%05s%.15s", $id_part, $rand_part);
}

this creates an UID with 5 chars base-36 primary id + 15 rubbish chars

stereofrog
Can I use your solution freely in commercial and public projects? I think this is the finally answer
ElAlecs
Hmm, i'm not a lawyer, but i think forum answers are public domain (http://en.wikipedia.org/wiki/Public_domain) by default. So yes, feel free to use it as you want ;)
stereofrog