[PHP] Unique ID - again
Hi folks, I'm using usual md5(microtime()); to create IDs, but now I've encountered a problem with that. I need to create explicitly 6 digit unique number(decimal). Yes, I know I can use for/while loop to fill a string with digits, but is it ABSOLUTELY sure that the random will never return same number when seed is microtime()? It is very important as the number will identify a bank transfer... Is there any other way than checking with all previous IDs? Or some MySQL function to do this for me? thank you Alex -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Unique ID - again
Hello Alex, Thursday, February 12, 2004, 11:00:41 AM, you wrote: A I'm using usual md5(microtime()); to create IDs, but now I've encountered a A problem with that. I need to create explicitly 6 digit unique A number(decimal). Yes, I know I can use for/while loop to fill a string with A digits, but is it ABSOLUTELY sure that the random will never return same A number when seed is microtime()? It is very important as the number will A identify a bank transfer... If the data is so important - why are you restricting it to a 6 digit key? You should ideally use the uniqid() function perhaps with a combined lcg entropy at the end. That will give you a 13 character long unique id - or md5 it for a 32 character one (see the manual for examples). A Is there any other way than checking with all previous IDs? Or some MySQL A function to do this for me? For something so important I would use (a) a longer unique ID and (b) I'd still run a SQL check to see if the ID has been already used or not. A simple MySQL count will bring that result back very quickly. -- Best regards, Richardmailto:[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Unique ID - again
MySQL (or another SQL database) is the answer. You can check for an existing OR use it's auto_increment or whatever else you like. Create a field to track all this stuff. There is no other way to guarantee that an ID is unique than to check it against existing. Random functions yield random results, so each time, there is ALWAYS a chance of a number being repeated. Microtime works pretty well, but there is STILL a chance two people could have a transaction at the same time. Yes, the odds can be insanely, insanely small for various functions (i.e. MD5) but as you shorten the length of your number, the odds get higher. And yes, it's important to realize that a 6 digit unique identifier is limited to 1 million possible combinations, not nearly enough for most banks. I might suggest you do some more PHP learning before you go and develop an online bank application - this stuff is pretty fundamental programming and database concepts. If your overall skill level isn't very high, you're likely to make mistakes, and either have errors, problems in functionality, or, worse, security problems. -Galen On Feb 12, 2004, at 3:00 AM, Alex wrote: Hi folks, I'm using usual md5(microtime()); to create IDs, but now I've encountered a problem with that. I need to create explicitly 6 digit unique number(decimal). Yes, I know I can use for/while loop to fill a string with digits, but is it ABSOLUTELY sure that the random will never return same number when seed is microtime()? It is very important as the number will identify a bank transfer... Is there any other way than checking with all previous IDs? Or some MySQL function to do this for me? thank you Alex -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php