-3

I am looking for the most reliable way to generate unique ids for a project where users can post content without getting duplicates. When the post is submitted I would like to generate a unique id for it to be referenced back to. Such as when loaded from a shared link which might look like

example.com/post/e8a42f290fd853daa666aeaefa607f42

Except I rather have shorter links.

The ids will probably be a combination of a user's username and the current time.

Although there are similar questions I haven't found one that provide answers that I think are best, most suggest using md5 then trim it down, except that would higher the chances of the same string being generated.

Edit:

I am not using auto increment because it will become harder to scale later on.

Dan
  • 1,100
  • 2
  • 13
  • 36
  • 1
    why would you want to do that? why not just use an auto_incremented method; I assume you're using a database for this? – Funk Forty Niner Feb 14 '18 at 00:48
  • 1
    For a fully unique identifier, try generating a GUID. See https://stackoverflow.com/a/26163679/823549 – 1000Nettles Feb 14 '18 at 00:50
  • `bin2hex(random_bytes($id_length/2));` donezo. – Sammitch Feb 14 '18 at 00:52
  • 1
    @FunkFortyNiner sequence generators are easy to start with, but are very difficult to scale effectively. Large, random[ish] IDs are somewhat more difficult to start with, but scale wonderfully. – Sammitch Feb 14 '18 at 00:54
  • @Sammitch `random_bytes()` is PHP 7 exclusive, as a side note ;-) – Funk Forty Niner Feb 14 '18 at 00:55
  • @FunkFortyNiner that's what [random_compat](https://github.com/paragonie/random_compat) is for. :P – Sammitch Feb 14 '18 at 00:56
  • I see @Sammitch B-) – Funk Forty Niner Feb 14 '18 at 00:57
  • `$unique = substr(uniqid(rand(), true), 16, 16); // 16 characters long` – Funk Forty Niner Feb 14 '18 at 00:57
  • 2
    @Daniel I like your forethought. You might want to put in a bit more legwork to define your scaling strategy now, and potentially design your IDs to match. [Pinterest put out an interesting article](https://medium.com/@Pinterest_Engineering/sharding-pinterest-how-we-scaled-our-mysql-fleet-3f341e96ca6f) about their sharding and ID generation which, while not something I would specifically recommend, contains quite a bit of food for thought. – Sammitch Feb 14 '18 at 00:58
  • https://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string – Funk Forty Niner Feb 14 '18 at 00:59
  • Use rand() and append time to generate a random number – Don'tDownvoteMe Feb 14 '18 at 01:06
  • @Sammitch They mention _"Finally, we needed a nice way to generate universally unique IDs (UUID) for all of our objects."_ So you think a UUID would be good, maybe ignore the length since collision seems more important. – Dan Feb 14 '18 at 01:10
  • @FunkFortyNiner Would Scotts last edit in his answer with a length of 32 be good for what I need? – Dan Feb 14 '18 at 01:22
  • I don't see why that wouldn't work for what you want; seems ok. There are other solutions in there also. Btw, how many writes (per second/minute/hour/day..) are we talking here? – Funk Forty Niner Feb 14 '18 at 01:31

1 Answers1

0

Have you looked into uniqid()?

http://php.net/manual/en/function.uniqid.php

bart
  • 14,958
  • 21
  • 75
  • 105
  • Did you read or know of the warning about it? – Funk Forty Niner Feb 14 '18 at 00:52
  • Yes, but what you want does not exist. You want short and unique, but you don't want to use MD5... – bart Feb 14 '18 at 00:54
  • I've down-voted your question because unclear expectations "best". – bart Feb 14 '18 at 00:55
  • @bart I have replaced "best" with words more explaining :) – Dan Feb 14 '18 at 00:57
  • Please explain why you don't like long md5 hash? And please explain how mission critical it is to avoid collision? Because you seem to prioritize shortness over collision, but then complain about a low risk of collision... – bart Feb 14 '18 at 00:59