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.
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.
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.
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
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.
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.
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.
To generate id like Flipkart , just add alphebets with random number .
$random_id = 'OD'.rand(999999999999999,10000000000000);
echo $random_id;
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.
This live website is powered by Techno Smarter QA. The complete source code is now available with a powerful admin panel, responsive design, future updates, and support.