views:

4792

answers:

11

I want to create a unique id but uniqid() is giving something like '492607b0ee414'. What i would like is something similar to what tinyurl gives: '64k8ra'. The shorter, the better. The only requirements are that it should not have an obvious order and that it should look prettier than a seemingly random sequence of numbers. Letters are preferred over numbers and ideally it would not be mixed case. As the number of entries will not be that many (up to 10000 or so) the risk of collision isn't a huge factor.

Any suggestions appreciated.

A: 

Take the 6 first characters?

Kieveli
As uniqid is based on timestamp the first 6 characters will be the same for quite a long time ;) Even if i took the last x characters or combined this some way i think there still is a cleaner approach. Something like 'x1f' would be nice.
Antti
+3  A: 

There are two ways to obtain a reliably unique ID: Make it so long and variable that the chances of a collision are spectacularly small (as with a GUID) or store all generated IDs in a table for lookup (either in memory or in a DB or a file) to verify uniqueness upon generation.

If you're really asking how you can generate such a short key and guarantee its uniqueness without some kind of duplicate check, the answer is, you can't.

Chris
+3  A: 

Letters are pretty, digits are ugly. You want random strings, but don't want "ugly" random strings?

Create a random number and print it in alpha-style (base-26), like the reservation "numbers" that airlines give.

There's no general-purpose base conversion functions built into PHP, as far as I know, so you'd need to code that bit yourself.

Another alternative: use uniqid() and get rid of the digits.

function strip_digits_from_string($string) {
  return preg_replace('/[0-9]/', '', $string);
}

Or replace them with letters:

function replace_digits_with_letters($string) {
  return strtr($string, '01234567890', 'abcdefghij');
}
RJHunter
This is pretty close to what i want. Airline ticket id's are a good example of this as well. Basically what i want is a good way to create this random ~3-5 character/digit code, which i can then convert to a string. Uniqid is otherwise fine, just too long.
Antti
+5  A: 

Make a small function that returns random letters for a given length:

<?php
function generate_random_letters($length) {
  $random = '';
  for ($i = 0; $i < $length; $i++) {
    $random .= chr(rand(ord('a'), ord('z')));
  }
  return $random;
}

Then you'll want to call that until it's unique, in pseudo-code depending on where you'd store that information:

do {
  $unique = generate_random_letters(6);
} while (is_in_table($unique));
add_to_table($unique);

You might also want to make sure the letters do not form a word in a dictionnary. May it be the whole english dictionnary or just a bad-word dictionnary to avoid things a customer would find of bad-taste.

EDIT: I would also add this only make sense if, as you intend to use it, it's not for a big amount of items because this could get pretty slow the more collisions you get (getting an ID already in the table). Of course, you'll want an indexed table and you'll want to tweak the number of letters in the ID to avoid collision. In this case, with 6 letters, you'd have 26^6 = 308915776 possible unique IDs (minus bad words) which should be enough for your need of 10000.

lpfavreau
+1  A: 

You could use the Id and just convert it to base-36 number if you want to convert it back and forth. Can be used for any table with an integer id.

function toUId($baseId, $multiplier = 1) {
    return base_convert($baseId * $multiplier, 10, 36);
}
function fromUId($uid, $multiplier = 1) {
    return (int) base_convert($uid, 36, 10) / $multiplier;
}

echo toUId(10000, 11111);
1u5h0w
echo fromUId('1u5h0w', 11111);
10000

Smart people can probably figure it out with enough id examples. Dont let this obscurity replace security.

OIS
Is there any way to use base_convert() to include upper and lower case letters and 0-9?Does base_convert($uid, 62, 10) work?
JoshFinnie
JoshFinnie: you'll have to make your own case sensitive function for higher base values then 36.
OIS
Thanks OIS! I will have to try and find one on the interwebs...
JoshFinnie
A: 
function rand_str($len = 12, $type = '111', $add = null) {
    $rand = ($type[0] == '1'  ? 'abcdefghijklmnpqrstuvwxyz' : '') .
            ($type[1] == '1'  ? 'ABCDEFGHIJKLMNPQRSTUVWXYZ' : '') .
            ($type[2] == '1'  ? '123456789'                 : '') .
            (strlen($add) > 0 ? $add                        : '');

    if(empty($rand)) $rand = sha1( uniqid(mt_rand(), true) . uniqid( uniqid(mt_rand(), true), true) );

    return substr(str_shuffle( str_repeat($rand, 2) ), 0, $len);
}
A: 

on my website my script does several math operations on the ID to generate a bigger int number which makes no sense at all for a guy who knows how to decode from base36, even tho this is not the safest way as OIS said, a smart guy could reverse my math algorithm easily with enough samples.

unKn
A: 

Here's the routine I use for random base62s of any length...

Calling gen_uuid() returns strings like WJX0u0jV, E9EMaZ3P etc.

By default this returns 8 digits, hence a space of 64^8 or roughly 10^14, this is often enough to make collisions quite rare.

For a larger or smaller string, pass in $len as desired. No limit in length, as I append until satisfied [up to safety limit of 128 chars, which can be removed].

Note, use a random salt inside the md5 [or sha1 if you prefer], so it cant easily be reverse-engineered.

I didn't find any reliable base62 conversions on the web, hence this approach of stripping chars from the base64 result.

Use freely under BSD licence, enjoy,

gord

function gen_uuid($len=8)
{
    $hex = md5("your_random_salt_here_31415" . uniqid("", true));

    $pack = pack('H*', $hex);

    $uid = base64_encode($pack);        // max 22 chars

    $uid = ereg_replace("[^A-Za-z0-9]", "", $uid);    // mixed case
    //$uid = ereg_replace("[^A-Z0-9]", "", strtoupper($uid));    // uppercase only

    if ($len<4)
        $len=4;
    if ($len>128)
        $len=128;                       // prevent silliness, can remove

    while (strlen($uid)<$len)
        $uid = $uid . gen_uuid(22);     // append until length achieved

    return substr($uid, 0, $len);
}
gord
for uppercase version, use this line instead - $uid = ereg_replace("["A-Z0-9]","",strtoupper($uid));
gord
A: 

Really simple solution:

Make the unique ID with:

$id = 100;
base_convert($id, 10, 36);

Get the original value again:

intval($str,36);

Can't take credit for this as it's from another stack overflow page, but I thought the solution was so elegant and awesome that it was worth copying over to this thread for people referencing this.

Adcuz
A: 

Yep, great solution, thanks gord.

KBala
Leave comments. Not answers as comments.
DMin
+1  A: 

@gen_uuid() by gord.

preg_replace got some nasty utf-8 problems, which causes the uid somtimes to contain "+" or "/". To get around this, you have to explicitly make the pattern utf-8

function gen_uuid($len=8) {

    $hex = md5("yourSaltHere" . uniqid("", true));

    $pack = pack('H*', $hex);
    $tmp =  base64_encode($pack);

    $uid = preg_replace("#(*UTF8)[^A-Za-z0-9]#", "", $tmp);

    $len = max(4, min(128, $len));

    while (strlen($uid) < $len)
        $uid .= gen_uuid(22);

    return substr($uid, 0, $len);
}

Took me quite a while to find that, perhaps it's saves somebody else a headache

Corelgott