Skip to main content
April 17, 2009
Question

Unique ID's in ColdFusion 8?

  • April 17, 2009
  • 3 replies
  • 5834 views

What tools are available to create UNIQUE ID's for an application?

I know in ColdFusion we can use CreateUUID() but this generates a string that is very long.

What I want to be able to do is create a string of say no more than 6 characters long that can be considered unique for a number of records that could reach the millions.

For example, the website TinyURL seems to do this quite well with a link like the following:

http://tinyurl.com/dlz23k

How do you think they ensure it's uniqu everytime??

Cheers,

Mikey

    This topic has been closed for replies.

    3 replies

    Inspiring
    August 3, 2009

    Greetings Kapitaine,

    Here some code I use to generate random unique numbers.

    Just some additional information for you to consider in your

    attempt to create an unique id.

    <cfscript>
        function getID_number(numberOfDigits) {
            id = "";
            for (i=1; i LTE numberOfDigits; i=i+1) {
                id=id & RandRange(0,9, "SHA1PRNG");
            }
            return id;
        }
    </cfscript>

    <cfset unique_num = #getID_number(6)#> <<<---- Change number to desired length

    You can add additional information before/after the unique number to further enhance

    the unique number.

    Leonard

    Inspiring
    April 17, 2009

    Kapitaine,

    Do these ID values need to be random?  If not you could use a table that has an autoincrement field.  To generate a new ID you insert a record then select the newly created ID value.  To include characters add a calculated column to the table that appends the numeric ID value to a string.  I can provide a quick sample if requested.

    April 17, 2009

    Hi,

    First of all, thanks guys for taking the time to help.

    While using an autonumber primary key ID would be the most logical and easiest solution - I would ideally not like people to simply hack the URL to guess other links. I know a 6 digit alpha numeric string wouldn't be fully un-guessable, but you know, it gives a little bit of a challenge.

    And besides that point, it would be a requirement to have a fixed length of 6 characters. If I used a primary key,the first would be 1 but the millionth user would have an ID of somepage.cfm/1000000 which could get hairy.

    So basically, th string needs certain requirements.

    1 - Must be 6 letters or numbers in length (for consistency)

    2 - Must be random (primary key for example would not be random)

    3 - Must be unique (like a UUID)

    I was thinking of doing something like #left(hash(),6)# but while that would look like it would work, I don't feel it would be unique.

    Hmm, any further ideas?

    Thanks,

    Mikey

    Inspiring
    April 17, 2009

    So much effort to avoid uuid's because they are so long.  Why is that such a big deal?

    Inspiring
    April 17, 2009

    Mikey,

    There's a function on CFLIB (among many, really) called getRandomString and you send in the length of the string:

    http://www.cflib.org/udf/getRandString

    I would think you could use this, generate a string, check it against a DB (to make sure it doesn't exist) and proceed from there as needed (get another random, use it, etc.).

    I'm sure there are better answers, too, but thought this might get you rolling...