×

Please Login or Register to continue.

2
(2.8K Views)

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 - 

OD273737673123662636263 

or 

878776236752721434

How can I create a random key number in PHP? 

like-

xbfh67dhs9yd

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

Share

2 Answers
(3.4K Points)
(edited)
2

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. 

<?php
function randomOrderId()
{
  $limit = 10;
   $rand_num=rand(pow(10, $limit-1), pow(10, $limit)-1);
   $add_time=time().rand(99,10); 
   $final_unique_id=$rand_num.$add_time; 
   return $final_unique_id;
}
$random_id= randomOrderId();
echo $random_id; 
?>

Output any random key like - 

2217038307164984888166

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. 

<?php 
function flipkartOrderId($type)
{
  $limit = 10;
   $rand_num=rand(pow(10, $limit-1), pow(10, $limit)-1);
   $add_time=time().rand(99,10); 
   $final_unique_id=$type.$rand_num.$add_time; 
   return $final_unique_id;
}
$flpkart_random_id= flipkartOrderId('OD');
echo $flpkart_random_id;
?>

Output any unique order id starts with OD - 

OD7434392562164984952911

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 - 
 

 flipkartOrderId('ODID')

 flipkartOrderId('ORDER')

etc 

How to generate a unique key in PHP? 

The unique key contains alphabets and digits. 

<?php
function randomKey($limit){
$values = 'ABCDEFGHIJKLMOPQRSTUVXWYZ0123456789';
$count = strlen($values);
$count--;
$key=NULL;
    for($x=1;$x<=$limit;$x++){
        $rand_var = rand(0,$count);
        $key .= substr($values,$rand_var,1);
    }

return strtolower($key);
}
echo randomKey(16);
?>

Output any unique key - 

odihqfl3xg3j7w29

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 - 

pb06luqb38pvbtm6hkpdqxy68lc3559izadlal03p0lmhl3b8w

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


Comment
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)
(edited)
0

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. 

 


Comment


Related Questions :-

Featured Items:-


Certificate and Marksheet system with verification & Franchise in PHP

$75



Certificate system in PHP website | PHP scripts

$22



Home Credito loan finance management multipurpose system in PHP

$65




Your Answer

You can post your answer after login..

Live Chat

Hello! How can we assist you today?