Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create unique IDs, like YouTube?

Tags:

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.

like image 247
Digerdoden Avatar asked Jul 02 '09 19:07

Digerdoden


People also ask

How is Youtube video ID generated?

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.

How do I create a unique ID in Google Sheets?

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!


2 Answers

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.

like image 57
chmac Avatar answered Oct 15 '22 19:10

chmac


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...

like image 36
DMCS Avatar answered Oct 15 '22 20:10

DMCS