ThunderPhoenix
ThunderPhoenix

Reputation: 1883

Generate random unique code for each user

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.

Upvotes: 1

Views: 1527

Answers (6)

stupidcoder
stupidcoder

Reputation: 21

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

Upvotes: 0

linktoahref
linktoahref

Reputation: 7972

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.

Upvotes: 1

user1205115
user1205115

Reputation:

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

Upvotes: 0

Yojan
Yojan

Reputation: 159

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.

Upvotes: 0

Shobi
Shobi

Reputation: 11461

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,

Upvotes: 1

Sapnesh Naik
Sapnesh Naik

Reputation: 11636

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

$unique = sha1(time);

Upvotes: 1

Related Questions