1

I would like to know how to generate in both PHP and SQL a unique random code for each user. This code will be used in the url of users' webpages.

For example, the website https://www.blablacar.fr/ uses the url https://www.blablacar.fr/user/show/random-unique-code-for-each-user to access to the profile of that user.

I think I should use uniqid php function and store it in the database. But I don't know there is a better way to do so.

Thank you very much for your help.

ThunderPhoenix
  • 1,649
  • 4
  • 20
  • 47
  • 6
    Possible duplicate of [How to generate unique random value for each user in laravel and add it to database](https://stackoverflow.com/questions/28524290/how-to-generate-unique-random-value-for-each-user-in-laravel-and-add-it-to-datab) – Sagar Gautam Jan 17 '18 at 05:44
  • 1
    Maintain table with increment 0000001 and when creating get the last id add one (+1) and insert new value to above table and use that ID to create user. 100% Unique – Abdulla Nilam Jan 17 '18 at 05:45
  • Here is package you can solve problem without making another unique id https://github.com/vinkla/laravel-hashids – Ramzan Mahmood Jan 17 '18 at 05:52

6 Answers6

1

You can use the current time to generate a unique string.

$unique = sha1(time);
Sapnesh Naik
  • 11,011
  • 7
  • 63
  • 98
1

https://github.com/ramsey/uuid,

ramsey/uuid is a PHP 5.4+ library for generating and working with RFC 4122 version 1, 3, 4, and 5 universally unique identifiers (UUID).

This package is used by dafult for generating uniqueids in laravel framework. if you are using laravel uniqid() function will give you the required unique id,

Shobi
  • 10,374
  • 6
  • 46
  • 82
1

You could make use of UUID. As mentioned by @Shobi P P Laravel uses the package ramsey/uuid

A PHP 5.4+ library for generating RFC 4122 version 1, 3, 4, and 5 universally unique identifiers (UUID).

So you could just import the package and use its function to generate an UUID

use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;

public function yourMethod()
{
    try {
        // Generate a version 1 (time-based) UUID object
        $uuid1 = Uuid::uuid1();
        echo $uuid1->toString() . "\n"; // i.e. e4eaaaf2-d142-11e1-b3e4-080027620cdd
    } catch (UnsatisfiedDependencyException $e) {
        echo 'Caught exception: ' . $e->getMessage() . "\n";
    }
}

PS:

Taylor Otwell has posted an poll on whether to include the UUID function to the Str class.

linktoahref
  • 7,812
  • 3
  • 29
  • 51
0

Random code can be generated by using str_random(length) function. You can generate a random code and attach the current time-stamp to that string. Which is unique I think.

Yojan
  • 159
  • 1
  • 10
0

Custom:

  function generateRandomStr($length = 8) {
        $UpperStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $LowerStr = "abcdefghijklmnopqrstuvwxyz";
        $symbols = "0123456789";
        $characters = $symbols.$LowerStr.$UpperStrl;
        $charactersLength = strlen($characters);
        $randomStr = null;
        for ($i = 0; $i < $length; $i++) {
            $randomStr .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomStr;
    }

    echo generateRandomStr(8).generateRandomStr(8);

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

0

You can use base_convert() function to generate unique id

base_convert ( string $number , int $frombase , int $tobase )

See the example code here: https://txeditor.com/jh6o8d1ldsd