Kabilan Gunasekaran
Kabilan Gunasekaran

Reputation: 31

How to generate unique random value for each user in symfony and add it to database

I am developing an enterprise based application. Here when the end-user registers via this application i will give a unique random number(8 digit), which I used to generate a 8-digit and mail it to him.

Expectation :

I want to make the number unique for each registered user. It should be random number

I already implement random number generator but its not unique for all. if any one knows please help me to solve this instead of random number generator, Any suggestion...?

Upvotes: 1

Views: 1784

Answers (2)

odan
odan

Reputation: 4960

You should generate a Universally unique identifiers (UUID). The ramsey/uuid libary is a good tool for that. Add another column to the "users" table with a UUID (e.g. user_uuid). Generate a new UUID and save the value there.

Example:

use Ramsey\Uuid\Uuid;

// Generate a version 4 (random) UUID object
$uuid4 = Uuid::uuid4();
echo $uuid4->toString(); // i.e. 25769c6c-d34d-4bfe-ba98-e0ee856f3e7a

Upvotes: 1

Edwin
Edwin

Reputation: 1125

There are several options like uniqid which generate an unique identifier. However uniqid has been proven to cause collisions in some cases. Adding true to the function call will add entropy to lower the amount of collisions. However I'd suggest you go with random_bytes or openssl_random_pseudo_bytes instead as they are profoundly better at it. :-)

Upvotes: 2

Related Questions