Generate unique ID in PHP
Posted by Raj
PHP UniqueID:
Generate unique ID in PHP
A unique ID can be created in PHP using the uniqid () function.
uniqid():Returns the unique identifier, as a string.
Here are some examples:
<?php
//creates a unique id with the 'unique' prefix
$a = uniqid('unique');
echo $a;
echo "<br>";
//creates a longer unique id with the 'unique' prefix
$b = uniqid ('unique', true);
echo $b;
echo "<br>";
//creates a unique ID with a random number as a prefix
$c = uniqid (rand (),true);
echo $c;
echo "<br>";
//this md5 encrypts the username from above
$md5c = md5($c);
echo $md5c;
?>
Using current time
<?php
//You can also use strtotime ("now");
$tn=substr(strtotime(now()),2,8);
echo($orderid);
echo "<br>";
?>
Using custom unique_code function
function unique_code()
{
$i=0;
$Unique_code="";
while($i<6)
{
$int = rand(0,9);
$a_z = "0123456789";
$rand_letter = $a_z[$int];
$Unique_code.=$rand_letter;
$i++;
}
return($Unique_code);
}
Generate unique ID in PHP
A unique ID can be created in PHP using the uniqid () function.
uniqid():Returns the unique identifier, as a string.
Here are some examples:
<?php
//creates a unique id with the 'unique' prefix
$a = uniqid('unique');
echo $a;
echo "<br>";
//creates a longer unique id with the 'unique' prefix
$b = uniqid ('unique', true);
echo $b;
echo "<br>";
//creates a unique ID with a random number as a prefix
$c = uniqid (rand (),true);
echo $c;
echo "<br>";
//this md5 encrypts the username from above
$md5c = md5($c);
echo $md5c;
?>
Using current time
<?php
//You can also use strtotime ("now");
$tn=substr(strtotime(now()),2,8);
echo($orderid);
echo "<br>";
?>
Using custom unique_code function
function unique_code()
{
$i=0;
$Unique_code="";
while($i<6)
{
$int = rand(0,9);
$a_z = "0123456789";
$rand_letter = $a_z[$int];
$Unique_code.=$rand_letter;
$i++;
}
return($Unique_code);
}
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
generate unique id php
.You can leave a response, or trackback from your own site.