Generate Single Check Serials

Asked

Viewed 758 times

3

Does anyone know how I can make a unique serial system in a way that never repeats the same serial, I’ve heard that with Rand and md5 can do but I’m not sure that one hour it won’t happen again.

  • Since you store the information, just do it sequentially, which will not have problem with repetition. Any hash you add will simply increase the risk of collision. Alternatively, you can use something based on the current time, as long as you’re careful about improper changes to the clock of the machine that generates the Ids. ( UNIQID() of PHP uses time as base)

  • If this serial is numerical, just use a sequence or something like this that will not have repeat problems, as @Bacco has already commented. If Serial needs to contain other characters, I advise you to use uuid. I use the class on this link http://br1.php.net/manual/en/function.uniqid.php#94959

  • It’s because I didn’t understand practically anything from the manual..

  • 1

    @Alfredolima will gradually get the hang of it. The important thing is to understand the advantages of each solution and apply the one that best suits you. You already have two starting points here. Anyway, the solutions presented are better than MD5

3 answers

4

Calling the function without any parameter will generate a unique 13-character ID.

I used this code and got the following results:

<?php

for ($i = 0; $i < 20; $i++)
{
    echo uniqid() , '<br />';
}

Answer:

4e7a2a9eda4f4
4e7a2a9eda513
4e7a2a9eda520
4e7a2a9eda52c
4e7a2a9eda538
4e7a2a9eda53e
4e7a2a9eda545
4e7a2a9eda54a
4e7a2a9eda54f
4e7a2a9eda553
4e7a2a9eda558
4e7a2a9eda55d
4e7a2a9eda562
4e7a2a9eda567
4e7a2a9eda56c
4e7a2a9eda571
4e7a2a9eda576
4e7a2a9eda57b
4e7a2a9eda580
4e7a2a9eda585

Source and more examples: http://www.phpit.com.br/artigos/gerando-numeros-de-identificacao-id-unicos-com-php.phpit

  • 4

    Not that the other answer is bad, but I really find more technical yours, because as I commented, the uniqid() is temporal, and does not repeat when used in normal conditions.

  • @Bacco agree there are several ways to do

3


You can generate a hash "unique" like that:

$serial = hash('sha512', mt_rand());
echo $serial;

Exit:

8011e6c104db93aeba14974ea471d592c7b3662d52e249be89e5bf978bd8bdf039613cfeb3e11dc9b962f2e953bf16ca56bc675d95dd67796413291f04174af4

If you make a strlen($serial) will see that 128 random characters will be generated, the chance of you being able to repeat these characters are 2 bilhões computed by the function mt_rand() from 0 to 2147483647.

3

I usually prefer something simple using timestamp

<?php
// retorna 10 caracters numéricos:
// exemplo: 1446815555
echo time();

The chances of collision

  1. It can occur when the environment is on the wrong date in the past, where at some point it can match some past timestamp already used. And even in this situation are very small odds.

  2. If used in a loop of repetition in very fast runs, the chance of collision is very large, as a loop of repetition can call the function time() more than once under the same millisecond.

For this case, of use within a repeat loop, one can avoid collisions by incrementing a counter

Example:

for ($i = 0; $i<5; $++)
    echo time().$i.PHP_EOL;

It will print something like

14468155550
14468155551
14468155552
14468155553
14468155554

Which style or technique to choose

Serial type choice depends on business model.

As an example, if the serial is used commercially, for customer support, for example, it is impossible to present the customer with such a serial 8011e6c104db93aeba14974ea471d592c7b3662d52e249be89e5bf978bd8bdf039613cfeb3e11dc9b962f2e953bf16ca56bc675d95dd67796413291f04174af4

Imagine the customer support asking to spell the serial number on the phone. rsrs

Finally, there is no better or worse. There is what is most suitable or less suitable for the business model.

However, in almost all cases, the smaller the serial, the better. Among the requirements of being unique and having low or no possible collision.

Life cycle of Timestamp

If you use timestamp, be aware at the end of your life cycle.

The timestamp is limited until 2038 and this is not far away. https://en.wikipedia.org/wiki/Year_2038_problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.