1

I'm trying to generate a unique ID in PHP for Orders.I came across several answers,of which uniqid found easy and interesting.But this warns me from using this.Does adding some random value to the uniqid would make it secure? If not is there any way to generate date,time,second and microsecond as orderid? Is that good or please help me find a better solution.

Community
  • 1
  • 1
Shan
  • 463
  • 3
  • 17
  • why not generate a hash based on the current timestamp converted to millis? – Praveen Puglia Feb 11 '15 at 07:27
  • @pjp can you please explain it as an answer – Shan Feb 11 '15 at 07:28
  • can't cause it's a duplicate question. already marked by someone you see. – Praveen Puglia Feb 11 '15 at 07:29
  • 1
    You can do something like this to generate random sequence: `sha256(time().rand(1, time())).rand(0, time())` (it will be almost impossible to generate same string again) – Justinas Feb 11 '15 at 07:39
  • i would suggest, `date(Ymd);` `strtoupper(substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 5)), 0 , 5));` and add both to get the one in 1000 combination with unrepeated and unique number – varun kumar Feb 11 '15 at 08:04
  • 1
    you can use php **uniqid** [http://php.net/manual/en/function.uniqid.php](http://php.net/manual/en/function.uniqid.php) . supports both PHP 4 and PHP 5 – Arun Yokesh Feb 11 '15 at 09:51
  • time is best unique id – Shaiful Islam Feb 11 '15 at 10:15
  • To everyone above: no, these are terrible sources of randomness and by far not guaranteed to be unique at all. Things don't get "more random" by chaining more barely-random functions together. It may be *very very unlikely* to randomly reproduce those values, but it's hardly impossible. Put those into an environment with high enough traffic and one day you'll be wondering about some bug stemming from colliding values. – deceze Feb 11 '15 at 11:26
  • And your solution is? – Mawg says reinstate Monica Feb 11 '15 at 14:52
  • 1
    http://php.net/manual/en/function.com-create-guid.php GUID - Globally Unique IDentifier – Mawg says reinstate Monica Feb 11 '15 at 14:53
  • @Mawg As laid out in the linked duplicate, a UUID generator is the best option. For example: http://pecl.php.net/package/uuid. If you google a bit, you can also find pure userland PHP implementations of UUID generators. Alternatively, use any cryptography-strength random number generator, e.g. `/dev/urandom`, and read a sufficient number of bytes from it. – deceze Feb 12 '15 at 02:47
  • @deceze i am currently using uuid generator `echo printf( '%04x%04x', mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ));` . But can you please explain why it 'guarantees' unique id .Is there ,exist the possibility of duplication. – Shan Feb 12 '15 at 05:04
  • 1
    @Shan That's not a real UUID generator at all. The UUID algorithm is designed specifically in a way that makes good use of randomness and produces values large enough that it's **very very unlikely** to ever produce the same value again. See http://en.wikipedia.org/wiki/Universally_unique_identifier#Random_UUID_probability_of_duplicates: *"Only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%."* – Can you make the same statement about your generator with any confidence? – deceze Feb 12 '15 at 05:12
  • (Hint: No, you cannot. If you'd generate 1 billion of your ids every second, you're guaranteed to hit a duplicate after just about 4 seconds.) – deceze Feb 12 '15 at 05:16
  • @deceze just for curosity,is this requires for an order id – Shan Feb 12 '15 at 05:25
  • The problem you're trying to solve is: how do I generate a unique id? There are only two real approaches: either a central coordinator is generating these ids one after the other, keeping track of generated ids and/or ensuring there are no duplicates. A MySQL auto increment column is such a thing. The other approach without such a central entity requires you to generate ids in a way that makes them so unlikely to ever duplicate that you can essentially assume they're guaranteed to be unique. If you're doing neither of these, nobody can guarantee what will happen and when something will break. – deceze Feb 12 '15 at 05:48

0 Answers0