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.