How to generate unique number 8 digits

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dungntr
Forum Newbie
Posts: 2
Joined: Thu Apr 12, 2007 3:57 am

How to generate unique number 8 digits

Post by dungntr »

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
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Code: Select all

$token = substr(md5(uniqid(rand(), true)), 0, 8);
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Oren wrote:

Code: Select all

$token = substr(md5(uniqid(rand(), true)), 0, 8);
How do you know that will be unique?

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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

It will be - in reality it will be and you know that.

Edit: But yes, personally, I'd check with the database before inserting it.
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

I use time(); somewhere in my "unique" string generator (I also use MD5 like the example above), because for some reason that number keeps increasing :roll:

I have also used microtime() for this. Both these functions work pretty good with uniqid as the prefix. :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If you are going to be cutting strings to a certain length from a longer length, you have no choice but check them against known values in the database. That is really the only way to be sure.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Oren wrote:It will be - in reality it will be and you know that.
This is sarcasm right?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

why not use an autoincremented field in your database?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Jcart wrote:why not use an autoincremented field in your database?
Security issue if it's being used to validate an email address or reset a password.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

aaronhall wrote:
Jcart wrote:why not use an autoincremented field in your database?
Security issue if it's being used to validate an email address or reset a password.
Of course.. but the user is asking for an 8 digit number, which is not much more *secure*. Which is why I asked.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Begby wrote:
Oren wrote:It will be - in reality it will be and you know that.
This is sarcasm right?
What the heck?? 8O
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Oren wrote:
Begby wrote:
Oren wrote:It will be - in reality it will be and you know that.
This is sarcasm right?
What the heck?? 8O
In reality, there is an off chance that there will be a collision. Probabilities don't lie.
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.
It was the "It use to authentic user" part that threw me. May as well find out.

dungntr: what are you using the number for?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Oren wrote:
Begby wrote:
Oren wrote:It will be - in reality it will be and you know that.
This is sarcasm right?
What the heck?? 8O
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.
dungntr
Forum Newbie
Posts: 2
Joined: Thu Apr 12, 2007 3:57 am

Post by dungntr »

OK, may be 8 digits or 8 char but it's unique.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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.
Post Reply