I've always wondered how and why they do this...an example: http://youtube.com/watch?v=DnAMjq0haic
How are these IDs generated such that there are no duplicates, and what advantage does this have over having a simple auto incrementing numeric ID?
How do one keep it short but still keep it's uniqueness? The string uniqid creates are pretty long.
So, in case you didn't know or weren't aware, youtube generates an 11-digit video id every time a user uploads a video. This 11-digit video id is a unique identifier for the video. How youtube does this is it uses base-64 encoding. Base-64 encoding is a system that uses 64 possible characters for each of the 11 digits.
You can generate a unique value using a formula in the spreadsheet. An ID must be a value, not a formula, though, so copy (Ctrl+C) and paste as plain text (Shift+Ctrl+V) the result of the formula calculation into the cell meant to contain the new ID. That's all there is to it!
Kevin van Zonneveld has written an excellent article including a PHP function to do exactly this. His approach is the best I've found while researching this topic.
His function is quite clever. It uses a fixed $index variable so problematic characters can be removed (vowels for instance, or to avoid O and 0 confusion). It also has an option to obfuscate ids so that they are not easily guessable.
Try this: http://php.net/manual/en/function.uniqid.php
uniqid — Generate a unique ID...
Gets a prefixed unique identifier based on the current time in microseconds.
Caution This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using random_int(), random_bytes(), or openssl_random_pseudo_bytes() instead.
Warning This function does not guarantee uniqueness of return value. Since most systems adjust system clock by NTP or like, system time is changed constantly. Therefore, it is possible that this function does not return unique ID for the process/thread. Use
more_entropy
to increase likelihood of uniqueness...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With