Short Unique Id in PHP

Short unique id in php

Make a small function that returns random letters for a given length:

<?php
function generate_random_letters($length) {
$random = '';
for ($i = 0; $i < $length; $i++) {
$random .= chr(rand(ord('a'), ord('z')));
}
return $random;
}

Then you'll want to call that until it's unique, in pseudo-code depending on where you'd store that information:

do {
$unique = generate_random_letters(6);
} while (is_in_table($unique));
add_to_table($unique);

You might also want to make sure the letters do not form a word in a dictionnary. May it be the whole english dictionnary or just a bad-word dictionnary to avoid things a customer would find of bad-taste.

EDIT: I would also add this only make sense if, as you intend to use it, it's not for a big amount of items because this could get pretty slow the more collisions you get (getting an ID already in the table). Of course, you'll want an indexed table and you'll want to tweak the number of letters in the ID to avoid collision. In this case, with 6 letters, you'd have 26^6 = 308915776 possible unique IDs (minus bad words) which should be enough for your need of 10000.

EDIT:
If you want a combinations of letters and numbers you can use the following code:

$random .= rand(0, 1) ? rand(0, 9) : chr(rand(ord('a'), ord('z')));

How to produce a short unique id in php?

Uniqid is not guaranteed to be unique, even in its full length.

Furthermore, uniqid is intended to be unique only locally. This means that if you create users simultaneously on two or more servers, you may end up with one ID for two different users, even if you use full-length uniqid.

My recommendations:

  • If you are really looking for globally unique identifiers (i.e. your application is running on multiple servers with separate databases), you should use UUIDs. These are even longer than the ones returned by uniqid, but there is no practical chance of collisions.

  • If you need only locally unique identifiers, stick with AUTO_INCREMENT in your database. This is (a little) faster and (a little) safer than checking if a short random ID already exists in your database.

EDIT: As it turns out in the comments below, you are looking not only for an ID for the user, but rather you are forced to provide your users with a random login name... Which is weird, but okay. In such case, you may try to use rand in a loop, until you get one that does not exist in your database.

Pseudocode:

$min = 1;
do {
$username = "user" . rand($min, $min * 10);
$min = $min * 10;
} while (user_exists($username));
// Create your user here.

Generating short unique reference id with PHP uniqid function

Thanks for all the suggestions. I have decided to use uniqid() with prefix as user_id. As uniqid() is system time dependent and can cause a conflict when 2 simultaneous requests hit the server, the user_id prefix will make sure that even if simultaneous request comes it will generate different ids since the user_id associated with each request will be different.

How can I make short random unique keys, like YouTube video IDs, in PHP?

The idea is to convert a unique integer (such as current time) in an other mathematical base.

A very simple way in PHP :

// With this precision (microsecond) ID will looks like '2di2adajgq6h'

// From PHP 7.4.0 this is needed, otherwise a warning is displayed
$cleanNumber = preg_replace( '/[^0-9]/', '', microtime(false) );
$id = base_convert($cleanNumber, 10, 36);

// With less precision (second) ID will looks like 'niu7pj'

$id = base_convert(time(), 10, 36);

short unique id mixed with custom id

Should be simple enough.

  1. Create a new table with recycled short ids

  2. Create another (sequence) table that holds the last id used for short id generation

  3. When a short id gets replaced by a custom one, insert it into the recycling bin

  4. When you need to create a new short id, look in the recycling bin first.

    a. if you can find an item, use that as the new short id (and remove it afterwards)

    b. if the bin is empty, increase the sequence by one and generate a new short id from that.

Update

This is a simple way to create and use sequence tables:

CREATE TABLE seqname (id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY) ENGINE=MyISAM SELECT 0 AS id;

UPDATE seqname SET id=LAST_INSERT_ID(id+1);

How do I make a short unique ID for each post in my database?

if each of your posts have an id already, and it is numeric, you can just encode them using an arbitrary base. think kinda like hex but with larger numbers..

check out this url by leah culver...

http://blog.leahculver.com/2008/06/tiny-urls-based-on-pk.html

for some more ideas. I've used this in the past and it works well. In leah's post it is base 56, so just take your primary key (Integer) and encode it into your new base 56, and you are all set.



Related Topics



Leave a reply



Submit