Page 1 of 1
How to generate unique number 8 digits
Posted: Thu Apr 12, 2007 4:03 am
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
Posted: Thu Apr 12, 2007 4:18 am
by Oren
Code: Select all
$token = substr(md5(uniqid(rand(), true)), 0, 8);
Posted: Thu Apr 12, 2007 5:10 am
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.
Posted: Thu Apr 12, 2007 5:21 am
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.
Posted: Thu Apr 12, 2007 12:46 pm
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
I have also used microtime() for this. Both these functions work pretty good with uniqid as the prefix.

Posted: Thu Apr 12, 2007 2:09 pm
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.
Posted: Thu Apr 12, 2007 4:02 pm
by Begby
Oren wrote:It will be - in reality it will be and you know that.
This is sarcasm right?
Posted: Thu Apr 12, 2007 4:04 pm
by John Cartwright
why not use an autoincremented field in your database?
Posted: Thu Apr 12, 2007 4:07 pm
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.
Posted: Thu Apr 12, 2007 4:27 pm
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.
Posted: Thu Apr 12, 2007 4:52 pm
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??

Posted: Thu Apr 12, 2007 5:10 pm
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??

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?
Posted: Thu Apr 12, 2007 6:53 pm
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??

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.
Posted: Thu Apr 12, 2007 10:17 pm
by dungntr
OK, may be 8 digits or 8 char but it's unique.
Posted: Thu Apr 12, 2007 11:59 pm
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.