How to generate reference number in php
I want to generate unique numbers so that I can use them as reference numbers to handle my client requests. Show I dont want the numbers to be repeated neither am i using any database to store the numbers. I simply want to use a simple algorithm to generate random numbers. I am using something like
Is it possible to do something like that. I know my requirement seems quite odd, but I just want to know if its possible. asked Feb 8, 2013 at 22:47
prakashchhetriprakashchhetri 1,7424 gold badges34 silver badges59 bronze badges 3 Why not just preface your random number with a Unix timestamp? This will ensure uniqueness.
Otherwise, you can store your numbers in a file. If you can, store them in a database. answered Feb 8, 2013 at 22:49
3 Why not use the function, php provides for that purpose: uniqid Example:
answered Feb 8, 2013 at 23:00
0
If you accept hexadecimal numbers, then:
5 bytes (i.e. 40 bits) creates about 2^40 (about a trillion) possible unique values. It should be more than random enough to create many unique IDs. answered Feb 8, 2013 at 23:19
Sverri M. OlsenSverri M. Olsen 12.8k3 gold badges35 silver badges51 bronze badges 1 ❮ PHP Misc Reference Definition and UsageThe uniqid() function generates a unique ID based on the microtime (the current time in microseconds). Note: The generated ID from this function does not guarantee uniqueness of the return value! To generate an extremely difficult to predict ID, use the md5() function. Syntaxuniqid(prefix,more_entropy) Parameter Values
Technical Details
❮ PHP Misc Reference
Scott-Cartwright/Getty Images Updated on October 02, 2018 A unique user ID can be created in PHP using the uniqid () function. This function has two parameters you can set. The first is the prefix, which is what will be appended to the beginning of each ID. The second is more_entropy. If this is false or not specified, it will return 13 characters; if it's true, 23 characters will be returned. Examples For Creating a Unique IDBelow are examples of creating a unique user ID, but each are a little different. The first creates a normal unique ID while the second shows how to make a longer ID. The third example creates an ID with a random number as the prefix while the last line can be used to encrypt the username before storing it. //creates a unique id with the 'about' prefix $a = uniqid(about); echo $a; echo " //creates a longer unique id with the 'about' prefix $b = uniqid (about, true); Echo $b; echo " //creates a unique ID with a random number as a prefix - more secure than a static prefix $c = uniqid (rand (),true); echo $c; echo " //this md5 encrypts the username from above, so its ready to be stored in your database $md5c = md5($c); echo $md5c; ?> |