I've been reading articles all day on the subject but am not sure how to proceed.
I have a MySQL table of users with a primary key of UUID_SHORT() values and an email field, which is also unique. I need to be able to generate an always unique, non-guessable ID to use in an activation URL. Something like this:
http://example.com/activate.php?id=tKd32f
After reading this question: How to code a URL shortener?
I've implemented a base58 encoding method that I'm trying to use with the UUID_SHORT() value representing the user, as generated by the MySQL server.
<?php
function base58_encode($num, $alphabet)
{
$base_count = strlen($alphabet);
$alphabet = str_split($alphabet);
echo "base_count: " . $base_count . "<br>";
$encoded = '';
while ($num >= $base_count)
{
$div = $num/$base_count;
echo "div: " . $div . "<br>";
$mod = ($num-($base_count*intval($div)));
echo "mod: " . $mod . "<br>";
echo "alphabet[$mod]: " . $alphabet[$mod] . "<br>";
$encoded = $alphabet[$mod] . $encoded;
echo "encoded: " . $encoded . "<br>";
$num = intval($div);
echo "num: " . $num . "<br>";
echo "------------------------------<br>";
}
if ($num) $encoded = $alphabet[$num] . $encoded;
return $encoded;
}
$alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
echo "WORKS => " . base58_encode(3707787591, $alphabet) . "<br><br>"; // some random number
echo "DOES NOT WORK => " . base58_encode(23298059492392988, $alphabet); // one of the unique IDs in my database
?>
If I use a 10 digit number, it works. But if I use the longer UUID_SHORT value, I get an undefined offset error because $num = intval($div) returns a negative number when its param is too large. I can't seem to figure out why, but it seems to be related to: http://ca1.php.net/intval
My questions:
Is this even the correct approach? Should I be using the UUID_SHORT value to generate the activation code or is that too guessable? If I do a SELECT UUID_SHORT() multiple times on the MySQL server, you can see this value increment by 1 (which worries me).
If this base58_encode is the correct approach, how would I resolve this error? How can I get a shorter activation code, not unlike goo.gl's url shortener service. How do they do it? I couldn't find a clear answer on this.
Should I be using something more along the lines of this? How to generate a secure activation string in php? However, this generates a really long ID. Not really necessary in my case and it makes for uglier activation URLs.
I plan on expiring my URLs after a certain period of time, but the IDs need to always be unique (and ideally, short enough). I also don't need to later decode the value because I will map the uniquely generated ID to the user ID in the database. What is the best approach here?
Thank you for your time.