Ways To Create Automatic Unique ID In PHP


There are many ways to create unique ID automatically in PHP. Some of those ways are:
Current Time and IP Style:
<?php
$time_stamp = date("Ymdhis");
$ip_add = $_SERVER['REMOTE_ADDR'];
$orderid = "$time_stamp-$ip_add";
$unique= str_replace(".", "", "$orderid");
echo $unique;
echo "<br/>";
?>

Custom Length:
<?php
//determine the length 
$length = '10';

//generate a random id. Encrypt it and set it as $random
$random = crypt(uniqid(rand(),1)); 

//if you want to remove any slashes
$random_no_slashes = strip_tags(stripslashes($random));

//remove any . and or reversing the string
$random_id = str_replace(".","",$random_no_slashes); 
$random_final = strrev(str_replace("/","",$random_id));

//take the first 10 characters from $random_final
$random_use = substr($random_final,0,$length); 

echo "Random Id: $random_use" ;
echo "<br>";
?>

uniqid() Function:
<?php
//Unique ID with 'cool' prefix
$u1 = uniqid(cool);
echo $u1;
echo "<br/>";

//Much longer unique ID with 'secret' prefix
$u2 = uniqid (secret, true);
echo $u2;
echo "<br/>";

//Unique ID with random prefix
$u3 = uniqid (rand(), true);
echo $u3;
echo "<br/>";

//Unique ID with md5 encryption
$u4 = md5($u3);
echo $u4;
echo "<br/>";
?>

XX-XXX-XX Style:
<?php
// Function to generate unique ID 
function StyleId() { 
    $create = strtoupper(md5(uniqid(rand(),true))); 
    $style = 
        substr($create,0,8) . '-' . 
        substr($create,8,4) . '-' . 
        substr($create,12,4). '-' . 
        substr($create,16,4). '-' . 
        substr($create,20); 
    return $style;
}
// End of function

$u = StyleId();
echo $u;
echo "<br/>";
?>


Good luck...

Labels: , , , , , , ,