php - Logical thing to do about checking for unique id? -
I have a randomly generated link id that is 8 digits long with 50 values per point, which means 4x10 ^ 13 possible combination I think). I have about ten thousand questions per day.
My question is, should I check 4 tables of each query for duplicates, or should I leave it? Or can it make 10 points so that it definitely does not match?
Edit:
Generates my (probably copied) generator
// START generates random string function genRandString ($ len = 8 ) {$ Base = 'ABCDEFGHKLMNPQRSTWXYZabcdefghjkmnpqrstwxyz23456789'; $ Max = strlen ($ base) -1; $ Randstring1 = ''; Mt_srand ((double) microtime () * 1000000); While (Stellan ($ randstring1) & lt; $ len + 1) $ randstring1 = $ Base {mt_rand (0, $ max)}; Return $ randstring1; } / END generates random strings
This pseudo random number depends on the quality of the generator Does. You may have insufficient entropy, so you are more likely to get a collision which you feel.
Is there a reason you are not using? It seems that the best solution designed for this purpose
However, I do not recommend checking the duplicates before inserting. This race is under the circumstances, i.e., after putting any duplicate value after you checked it, but before inserting it. So you have to handle the duplicate key violation exception anyway. To try simply inserting (do not check first) and handle exceptions as needed.
Your comments and your algorithm again: I will not use that hashing scheme, your 50 different values are less than 24 bits in four digits. So you have very important after a few thousand lines in your database.
How about this solution: Use the growing primary key value of a monotonically, like AUTO_INCREMENT. To convert this number to alphanumeric string, use
base_convert ()
:$ id = 12345678; $ Str = base_convert ($ id, 10, 36); Repeat "$ str \ n";
The result is
7clzi
.If you are concerned about confusion in letters like 1, L, I, 0, O, you can make some custom replacements:
$ = from array ( '1', 'l', 'i', '0', 'o'); $ Array = array ('a', 'b', 'c', 'd', 'e'); $ Str = str_replace ($ to $$, $ str);
Now value is converted to
12345678
to7cBzC
. When someone requests a page via this code, then reverse the conversion:$ code = str_replace ($ to $ $, $ code); $ Id = base_convert ($ code, 36, 10);
Comments
Post a Comment