How to generate unique number 8 digits
Moderator: General Moderators
How to generate unique number 8 digits
How to generate unique number 8 digits
I want a unique number with 8 digits to insert into my table. It use to authentic user
help me soon
Thankyou
I want a unique number with 8 digits to insert into my table. It use to authentic user
help me soon
Thankyou
Code: Select all
$token = substr(md5(uniqid(rand(), true)), 0, 8);How do you know that will be unique?Oren wrote:Code: Select all
$token = substr(md5(uniqid(rand(), true)), 0, 8);
I'd do one of two things.. either use a while loop to keep generating tokens and checking whether they're in the database or not until a unique one is found, or prefix the token with the auto incremented id of the record.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
In reality, there is an off chance that there will be a collision. Probabilities don't lie.Oren wrote:What the heck??Begby wrote:This is sarcasm right?Oren wrote:It will be - in reality it will be and you know that.
It was the "It use to authentic user" part that threw me. May as well find out.Jcart wrote:Of course.. but the user is asking for an 8 digit number, which is not much more *secure*. Which is why I asked.
dungntr: what are you using the number for?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I think this has to do with the fact that you are taking the first 8 chars of an md5'ed string, which means that you could potentially have an unlimited number of collisions for that substring since MD5 hashes are 32 chars by default. That means that chars 0-8 of any number of strings could be potentially the same while the remaining 24 chars could be different.Oren wrote:What the heck??Begby wrote:This is sarcasm right?Oren wrote:It will be - in reality it will be and you know that.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
With any limitation is length there is no way to guarantee unique values 100% of the time. The period of time you can generally think it's going to be unique on the first attempt is loosely based on how large your limit is, but there is other things you cannot really control at play too.
It's simply best to check for existing values before you go any further.
It's simply best to check for existing values before you go any further.