Reputation: 11
I am using a home made license generator and would like to use the uniqid function to generate some license strings
I can already generate some ids but since I am truncating them to avoid havind the first part always the same, I would like to double-check if the generated id is already existing in the database.
I am pretty new to php so a litlle help to create this function would be really appreciated
I have already done this:
substr(uniqid($lic_key_prefix, true), 7);
I would like a function that would look into the database on a recursive way until it finds a very uniq one id to my database items.
Anyone have a hint ?
Upvotes: 1
Views: 1480
Reputation: 24363
If all you are doing is trying to prevent people from guessing the next ID or learning a specific pattern from the output of uniqid()
, truncating it is not a good idea. I had to do something similar before and I settled on Hashids. It generates two-way hashes based on whatever integer you give it and the output strings are completely unrelated to the others and it would be impossible to determine the next value based on the current one unless the secret string is known.
// Include necessary files:
require_once('lib/Hashids/HashGenerator.php');
require_once('lib/Hashids/Hashids.php');
// First you set a secret string. Change this to whatever you want.
$secret = 'my_secret_string';
// Then create a hash starting with 2 characters for the first one.
// This will get longer as the id increases.
$hashids = new Hashids\Hashids($secret, 2);
// Create the first 20 hashes based on IDs 0 to 19
for ($i=0; $i<20; $i++) {
echo $hashids->encode($i) . "\n";
}
// Or if you want each hash to be a minimum of 32 characters:
$hashids = new Hashids\Hashids($secret, 32);
for ($i=0; $i<20; $i++) {
echo $hashids->encode($i) . "\n";
}
Here is the output of the above:
Yb
JE
oZ
n1
OX
QO
vx
X4
xP
k8
Wx
V9
mz
DL
wA
Eo
4B
1Z
8W
zE
eXZ9L0gO1Bvwx7qYblQbop5VAzDRn2M3
e8RWd7PjMQZGbLaJElmkAyEJn5gY2zDV
QKJMW3XEZ1vzgjqoZNA6G9xmpO8rV0nP
RE3p0moweBK1XAqn1NvG4bjnMD5Z89kV
rKEvgR5Y2djmp7aOXNnbzO8xXQVe0WMB
zeLmYjv0AG3MK7lQON1QopRJODB5w48E
8WoLRyJngvZY63Nvxlwp7ed5MrPVBm4j
EOvxdnV3kB9jP2aX4am5bGyXRwzLo4ZD
YrAvRL7dKV4WxQNxPN1bM3yXog58D9BJ
yJ3WQ6ZR1Bk75eNk8q0mPb8K4dxwMYGA
OVbKy1GBmzpewXqWxlARP02M57nx43Zk
vDxbYGWOJyQ5rmqV9NLoBez6137VPEXR
Qwn6LPjx8g0XzBqmzlbde7EW2RrpkGKM
Gzk5MRXm78JQ1rqDLqY0Kx2go4dnB3Ee
V0p7Z2QwjdeY5RqwAazMWvJPnokKAb1D
BAYjp5DOJnVKgXNEolkGMQx7Rm2weor6
OXrPA8VRJkzvbWN4BlnG4QBeDxM15L0K
r7QEdMoJW8Gzgxl1Za12eK4vOYLR5Vb9
57ZVbnWOjQJGdBl8WqXYL8w61Ey2Kxp4
omOyWjPJ71Dk30qzENBZY26Mr9dAVgbE
Of course, you can then decode any of the hashes back into a regular integer. So all you need to do is pass the ID from the database into this function and give that to the client. Then when the client provides you with a hashid, you put it into the decode method and get the actual ID. That way you don't need to worry about trying to do multiple queries if the truncated uniqid already exists, because hashids are guaranteed to be unique.
Upvotes: 4
Reputation: 1733
You could add a unique key to the id field in your database, then when inserting check if it was successful or not. If not, change the unique id, then insert again. Put this in a loop until it is successful. This would guarantee that every id is unique.
Upvotes: 2