1

I need create unique user id, which wont replicate at any cost. As, i am using this id to make user to login, and this also work as reference id. The id can be 6 to 8 of length. So far now, i have tried this

 $v = mt_rand(10, 99);
 $b = date("s");
 $a = round(microtime(true));
 $c = substr($a, 8);

Will this create unique id ? And , am expecting user base would cross 10 millions. So need to without collision for more than 10 million records.

Dazzile Pro
  • 253
  • 3
  • 13
  • 1
    You should be using a UUID. https://en.wikipedia.org/wiki/Universally_unique_identifier It's not 6-8 characters, but 6-8 characters isn't really enough if you want to guarantee uniqueness, unless you're storing previously used values somewhere. – Brad Jul 28 '19 at 07:04
  • Yes, i am storing that as member id's in database ! UUID is too long, it wont be feasible for users to login using UUID – Dazzile Pro Jul 28 '19 at 07:39
  • Should i save the previously used in separate table ? – Dazzile Pro Jul 28 '19 at 07:43

4 Answers4

2

Here you may do like this.

md5(uniqid(rand(), true))

Please check PHP: How to generate a random, unique, alphanumeric string?

Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
1

you can simply use php uniqid() function to generate unique id.

for more infomration about uniqid() function refer below link

https://www.php.net/manual/en/function.uniqid.php

note: above function also not guarantees always return the unique value Since most systems adjust system clock by NTP or like, system time is changed constantly. to increase the uniqueness set the more_entropy to TRUE

uniqid ([ string $prefix = "" [, bool $more_entropy = FALSE ]] ) : string
Theekshana
  • 417
  • 4
  • 8
0

You cannot rely on a solution of your own to get 100% unique IDs considering the length you are looking at. I suggest that you should use an auto-incremented primary key and after creating the record, update your custom ID column with a value created using the primary key.

For example; create a record and get its integer ID. And then update the custom ID column something like this:

$user = User::create($data);
$user->custom_id = sprintf('user-%s', $user->id);
$user->save();

This way, you will be able to use a short ID and will also ensure that the ID is unique.

But as suggested in other answers, you should be using a UUID library. That's the solution that has been developed after years of use cases.

p.s. With your own solution that you specified, you will experience 1000s of collisions before reaching the 10 million target. Moreover, I used User::create() as an example to create a record just because you have tagged this with laravel.

Rehmat
  • 4,681
  • 3
  • 22
  • 38
0

As I mention in another question:

The answer is to use a unique identifier that combines a "unique" part and a "random" part.

  • The "unique" part can be a monotonically increasing counter (such as the record or row number in a database), or it can be a number generated with a full-period linear congruential generator (which cycles pseudorandomly through all possible values in its period before repeating).
  • The "random" part is simply a random number generated with a cryptographic random number generator [random_int or random_bytes in PHP]. In general, the longer it is, the less predictable it will be.

Moreover, for the purposes of generating unique identifiers, there is little reason to limit yourself to 8-digit identifiers as you're now doing.

Peter O.
  • 32,158
  • 14
  • 82
  • 96