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.

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

rand[10*45, 100*98]

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.

$random = time[] . rand[10*45, 100*98];

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:

uniqid[rand[10*45], true]

answered Feb 8, 2013 at 23:00

0

If you accept hexadecimal numbers, then:

function random_id[$bytes] {
    $rand = mcrypt_create_iv[$bytes, MCRYPT_DEV_URANDOM];
    return bin2hex[$rand];
}
echo random_id[5]; // Ex: a06e0e4e72

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 Usage

The 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.

Syntax

uniqid[prefix,more_entropy]

Parameter Values

ParameterDescription
prefix Optional. Specifies a prefix to the unique ID [useful if two scripts generate ids at exactly the same microsecond]
more_entropy Optional. Specifies more entropy at the end of the return value. This will make the result more unique. When set to TRUE, the return string will be 23 characters. Default is FALSE, and the return string will be 13 characters long

Technical Details

Return Value:PHP Version:Changelog:
Returns the unique identifier, as a string
4+
The prefix parameter became optional in PHP 5.0.
The limit of 114 characters long for prefix was raised in PHP 4.3.1.

❮ PHP Misc Reference


How to Generate a Unique ID in PHP

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 ID

Below 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; ?>

Chủ Đề