unique code

Discussion in 'PHP' started by roice, Nov 6, 2011.

  1. #1
    Hello all,
    I want create website that create unique code for coupons (6-7 notes from numbers and letters).
    Every code must be unique whice mean that it can't repeat twice.
    Can you give me some advice how to do it?

    Thank yo in advanced,
    Roi.
     
    roice, Nov 6, 2011 IP
  2. amgadhs

    amgadhs Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can use PHP's mt_rand() function to generate random numbers. You can specify the minimum and maximum values too. And then save the number in a MySQL table. So each time a number is generated check if exists in the table before using it. Hope that helps
     
    amgadhs, Nov 6, 2011 IP
  3. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your advice.
    I'm don't think that this way is the ideal way
    I was thinking about taking lats 6 numbers of LINUX time. Or taking the id of the previous copoun code (from the DB) and generate it with md5...
     
    roice, Nov 6, 2011 IP
  4. JimmyRP

    JimmyRP Active Member

    Messages:
    456
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #4
    yeah you could grab the time, generate the md5 of them time, and then pick 6 or 7 characters starting at a random spot in the md5 time using the mt_rand()
     
    JimmyRP, Nov 6, 2011 IP
  5. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If I'll take only the 6-7 numbers (and not all of them) I might get an un-unique number...
     
    roice, Nov 6, 2011 IP
  6. Divided

    Divided Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    3
    Trophy Points:
    0
    #6
    What ever you do you will have to test to make sure you do not already have that key stored in the database. Using something like the time and a random hash is a good way of getting a unique key but you must check to be sure.
     
    Divided, Nov 7, 2011 IP
  7. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi Divided,
    OK, I do understand, but is there any way I can create unique number - number that doesn't repeat more than once...?
     
    roice, Nov 7, 2011 IP
  8. subdivisions

    subdivisions Well-Known Member

    Messages:
    1,021
    Likes Received:
    40
    Best Answers:
    1
    Trophy Points:
    145
    #8
    Seriously? Nobody got this?

    PHP's uniqid() function will generate a string of letters and numbers based on the current time, so it'll never duplicate.

    If you just want numbers, use time().
     
    subdivisions, Nov 7, 2011 IP
  9. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks subdivisions,
    This is what I was looking for.
    Question - I want it to be only 6 notes. If I'll cut (substring) the result of uniqid() function I may get an un-unique string/number, no?
     
    roice, Nov 7, 2011 IP
  10. subdivisions

    subdivisions Well-Known Member

    Messages:
    1,021
    Likes Received:
    40
    Best Answers:
    1
    Trophy Points:
    145
    #10
    Changes are almost zero, but it isn't impossible. Is there any way you can adjust to take the whole string?
     
    subdivisions, Nov 7, 2011 IP
  11. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    How many letters this function (uniqid()) will bring me?
     
    roice, Nov 8, 2011 IP
  12. retwedia

    retwedia Member

    Messages:
    196
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    48
    #12
    This should help you completely. I wrote a function to check for duplicate coupon codes AND a function to generate random codes with are alphanumeric.
    Enjoy!

     
    retwedia, Nov 8, 2011 IP
  13. David Lemarier

    David Lemarier Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Use the time() function - the best method to get unique value ;)

    example:

     
    David Lemarier, Nov 8, 2011 IP