Please Login or Register to continue.


I am creating an eCommerce website using PHP and MYSQL databases. I want to create a unique random id like flipkart.com.

Order id or any random unique key like - 




How can I create a random key number in PHP? 



The rando key should be contained digits and alphabets in PHP. This random id or key should be unique on every execution. 

I want to create a random id or token key for an eCommerce system using PHP programming. Flipkart provides a unique order number when users purchase any product from an eCommerce shop. I like that unique random order number. I want to create a unique random order number for my eCommerce project in PHP. 

(2.4K Points)
in PHP


2 Answers
(3.4K Points)

The random id is used to give an id automatically which is not available before. You can create random order id like Flipkart and any id with random numbers. 

How to create a random id using PHP? 

First of all, we will create a random id using PHP with digits only. The random id will contain only digits. 

function randomOrderId()
  $limit = 10;
   $rand_num=rand(pow(10, $limit-1), pow(10, $limit)-1);
   return $final_unique_id;
$random_id= randomOrderId();
echo $random_id; 

Output any random key like - 


In the example above, we created a function to return any random id- 

We used time() function to get current time timestamps . We used the rand() function with time() function to generate random number in PHP and also small rand(99,10); numbers with time() because at the same time many users can order products then here random numbers will create a random id after time() values. The time() function return Unix timestamp of current time  and the rand() function returns the random numbers.

How to create a random order id like Flipkart? 

if you want to create a unique order id like Flipkart website using PHP then you should follow the function below. The function will help you to generate a unique Order id like Flipkart. 

function flipkartOrderId($type)
  $limit = 10;
   $rand_num=rand(pow(10, $limit-1), pow(10, $limit)-1);
   return $final_unique_id;
$flpkart_random_id= flipkartOrderId('OD');
echo $flpkart_random_id;

Output any unique order id starts with OD - 


As you can see, we created flipkartOrderId() function to generate a unique random order id like flipkart.You can set any limit and type in the function or time of execution like - 




How to generate a unique key in PHP? 

The unique key contains alphabets and digits. 

function randomKey($limit){
$count = strlen($values);
        $rand_var = rand(0,$count);
        $key .= substr($values,$rand_var,1);

return strtolower($key);
echo randomKey(16);

Output any unique key - 


If you want to generate a token with it, then increase the limit like - 

echo randomKey(50);

You will get a random token id like this - 


In this way, you can generate a unique random id and unique order id like Flipkart and token id using PHP.

Thanks for these functions. Now, I can create a random id, key, or order id like the Flipkart eCommerce platform.
arnabisaha 13 Apr 2022

(9.4K Points)

I use PHP rand() function to generate a random id in PHP. This is one of the best ways to generate a random id or unique id using rand() function in PHP. 

Let's generate a random id. 

Generate unique random id using PHP rand() function

To generate a random id, we use the rand() function with arguments. 

$random_id = rand(999999999999999,10000000000000); 
echo $random_id; 

Now, you can run and get a unique random id on every execution. 

Create an order id like Flipkart - 

To generate id like Flipkart , just add alphebets with random number . 

$random_id = 'OD'.rand(999999999999999,10000000000000); 
echo $random_id; 

Create a unique token key - 

The token key contains the alphabet and digits. 

Token key like - ajsahn7xds222nvn32fbfr23v23

Example - 

$token = bin2hex(random_bytes(50));
echo $token; 

Now, you are able to generate a token key using PHP. You can use it to forget the password system in PHP. 



Related Questions :-

Featured Items:-

Certificate system in PHP website | PHP scripts


Home Credito loan finance management multipurpose system in PHP


Result management system with Marksheet in PHP website | PHP Scripts


Your Answer

You can post your answer after login..