Hi all i need to generate a 10 digit (at least) alphanumeric id when a customer submits a form, it has to be unique.

Is there an easy way of doing this or do i just use the uniqid function and then check against the database that it doesnt already exist

Regards

Dani AI

Generated

For a short, public-facing alphanumeric ID (>=10 chars) there are three practical choices depending on requirements:

  • Use an auto-increment numeric primary key if the value is purely internal (). It is simplest and fastest, but predictable.
  • Use a collision-resistant random token when the ID must be unguessable (, ). Make it cryptographically secure and enforce uniqueness at the database level.
  • Use a UUID/ULID-style identifier for universal uniqueness across systems (). UUIDs are longer; ULIDs or Snowflake-style IDs trade length for lexicographic ordering and compactness.

A 10-character string drawn from 62 symbols (0-9A-Za-z) gives 62^10 ≈ 8.39e17 possibilities, roughly 59.5 bits of entropy. That is usually fine for small to medium sites, but for very large scales choose more entropy (12+ chars or 128-bit tokens) to reduce birthday-collision risk. Never rely on a pre-check query to guarantee uniqueness — use a UNIQUE index and handle duplicate-key errors on insert to avoid race conditions.

Example workflow (practical, safe):

  • Generate a cryptographically secure base62 string of the requested length.
  • Attempt the INSERT into a table with a UNIQUE constraint on the ID.
  • On duplicate-key error, regenerate and retry a few times; fail loudly after N attempts.

Code example (PHP/PDO):

function genId($len = 10) {
    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $id = '';
    for ($i = 0; $i < $len; $i++) {
        $id .= $chars[random_int(0, 61)];
    }
    return $id;
}

// try insert with UNIQUE constraint; on SQLSTATE 23000 regenerate and retry

Notes: prefer random_int/random_bytes over rand/mt_rand for unpredictability; store the ID in a CHAR column sized to your generator; index it with UNIQUE. For reference on UUIDs and secure token guidance see the UUID spec (RFC 4122) and OWASP token/session guidance (OWASP Session Management Cheat Sheet). , , , , and offered useful starting points — combine secure generation with DB-level uniqueness for a robust solution.

Recommended Answers

All 7 Replies

Hi,

If you're storing the id in a database, why not use an auto-increment ID field?

R.

Try doing this:

uniqid(mt_rand(), true);

Thankyou for all the great suggestions their all perfect for what i want

Hello Dendixon

Here is the code as per you requirement.
It is more than 10 digit but you can configure the loop..

<?php
for ($i=0; $i<7; $i++) {
    $d=rand(1,30)%2;
//    echo $d ? chr(rand(65,90)) : chr(rand(48,55)).rand(01,99).$d;
	$w=$d ? chr(rand(65,90)) : chr(rand(48,55)).rand(01,99).$d;
	echo $w;
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.