Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: Unique Codes

  1. #1
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts

    Unique Codes

    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?

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Quote Originally Posted by NancyJ
    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:
    http://www.codeave.com/asp/code.asp?u_log=46

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

  • #3
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    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

  • #4
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #5
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    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.

  • #6
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    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'

  • #7
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes you key on the code. You check if it exists else you make a new one.
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #8
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    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.

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

    and set the code to collate CS not CI

    to make the code:

    do
    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)

  • #9
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •