Question

I'm trying to create reference numbers for customers and orders based on this answer with the following requirements:

  • All numbers
  • Unique (as possible without checking the database)
  • Same length
  • Customers start with 10
  • Orders start with 20

Here is my current code for a customer ref:

$ref = uniqid();
$decimalRef = hexdec($ref);
$finalRef = 10 . $decimalRef;

Is this a good solution? I'm worried about uniqueness and not sure if they will always be the same length.

Update

I don't want to use a database auto increment value because I don't want to write to the database beforehand, and I don't want my customers knowing they are my customer number 2. For obvious reasons.

Update 2

If I was to use a database auto-increment value, how do I then convert this into a more appropriate customer facing number as per the requirements (same length, starting with 10). If I only have 4 orders in the system, I don't want to announcing that to my users.

Was it helpful?

Solution

The uniqid() function generates a unique ID based on the microtime

This means if you can make sure no concurrent request for uniqid generation, it will be fine on that one server (because it bases on the system's time).

My suggestion is to combine it with any user session id or user id if they are available, as it is safe to assume that no one user can make two concurrent requests at the same time.

"10". hexdec(uniqid()).$userId ;//or hexdec(session_id()) instead of $userId

Updated: I am afraid that the hexdec function will return a value too big for the Int type. This is really an overkill. If the only concern is the order number, one can prefix the order number with a timestamp or similar or make the autoincreament start from 2000 instead of 0.

OTHER TIPS

You can try this.I am not sure about the uniqueness of this but is working form me.

// Generate a random character string
    function rand_str1($length = 8, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890',$type)
    {
        // Length of character list
        $chars_length = (strlen($chars) - 1);

        // Start our string
        $string = $chars{rand(0, $chars_length)};

        // Generate random string
        for ($i = 1; $i < $length; $i = strlen($string))
        {
            // Grab a random character from our list
            $r = $chars{rand(0, $chars_length)};

            // Make sure the same two characters don't appear next to each other
            if ($r != $string{$i - 1}) $string .=  $r;
        }

        // Return the string
        if($type=="Customers")
        {
        return "10".$string."";
        }
        else if($type=="Orders")
        {
        return "20".$string."";
        }
    }

Ok so to ensure the reference number is unique I should use an auto-increment database ID, not create my own.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top