View Full Version : Unique Codes

08-25-2005, 03:59 PM
I want to generate a set of unique codes (like coupons or serial keys) so that they can only be used once. Can anyone give me a shove in the right direction?

08-25-2005, 04:18 PM
I want to generate a set of unique codes (like coupons or serial keys) so that they can only be used once. Can anyone give me a shove in the right direction?

Generating random numbers in ASP:

Otherwise, use a database and autonumber/identity fields. Depends what you're trying to do.

08-25-2005, 04:22 PM
random != unique :P

and the identifier column of a db, although unique would only be a number not a code.
This is for promotional codes, so the idea is that you cant just guess a number and put it in and voila! - discount

08-25-2005, 05:03 PM
Make a table With pCode, Used, DateCreated, and DateUsed.

Key on pCode.

Randomly make a string and insert it into the table (rString, 0 , Getdate(), null)

Then you can tell what is going on and when it happened. You might also want to track the IP address but that is up to your own discretion.

08-25-2005, 05:27 PM
I dont see how that would help... I dont see anything unique there, the code part (the bit I'm looking for help with) you just say 'randomly make a string'... as previously mentioned, random is not the same as unique.
What is see here is just that the database choke when I tried to insert a code that was already in use.

08-25-2005, 06:06 PM
I have written my own method of doing this

Using a random word (provided by user creating the promotion) which is constant for every code in the promotion, a unique ID from the DB identity field, then encoded using my own method (based on ROT13)
Each code will be unique and not easy to 'crack'

08-25-2005, 07:26 PM
Yes you key on the code. You check if it exists else you make a new one.

08-26-2005, 08:40 PM
i believe you can do a unique random identifier as an index (guid) in mssql at least.. this is unique.. however due to the size of this i would more likely suggest that you generate a large alpha string and simply check its never been used or generated before by checking its existance in the database. you could also make this case sensitive to increase the number of possible solutions greatly.

ID Identifier (1, 1), code char(30), used bit

and set the code to collate CS not CI ;)

to make the code:

until createcoupon()

or something, and just make createcoupon return true if its made a unique one and inserted or false if its made one thats already in use (i never use do loops so that might not be right hehe)

08-27-2005, 09:27 AM
The method I'm using no longer needs to be 100% unique because its an encoded string that contains a password specific to the promotion and the id number of the specific code, the id number, password and full code must match in order to be valid. So far its working well. I plan to post a test area on my host to see if anyone can crack it - how long it takes and how they did it :)