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.
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
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...
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()
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.
Hi Divided, OK, I do understand, but is there any way I can create unique number - number that doesn't repeat more than once...?
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().
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?
Changes are almost zero, but it isn't impossible. Is there any way you can adjust to take the whole string?
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!