1. Menggunakan fungsi uniqid()
<?php
//creates a unique id with the 'about' prefix$a = uniqid(about);
echo $a;
echo "<br>";
//creates a longer unique id with the 'about' prefix$b = uniqid (about, true);
echo $b;
echo "<br>";
//creates a unique ID with a random number as a prefix - more secure than a static prefix $c = uniqid (rand(), true);
echo $c;
echo "<br>";
//this md5 encrypts the username from above, so its ready to be stored in your database$md5c = md5($c);
echo $md5c;
echo "<br>";
?>
2. menggunakan current time + IP style
<?php
//You can also use $stamp = strtotime ("now"); But I think date("Ymdhis") is easier to understand.$stamp = date("Ymdhis");$ip = $_SERVER['REMOTE_ADDR'];$orderid = "$stamp-$ip";$orderid = str_replace(".", "", "$orderid");
echo($orderid);
echo "<br>";
?>
3. Generate unique yang bisa di batasi panjangnya
<?php
//set the random id length $random_id_length = 10;
//generate a random id encrypt it and store it in $rnd_id $rnd_id = crypt(uniqid(rand(),1));
//to remove any slashes that might have come $rnd_id = strip_tags(stripslashes($rnd_id));
//Removing any . or / and reversing the string $rnd_id = str_replace(".","",$rnd_id); $rnd_id = strrev(str_replace("/","",$rnd_id));
//finally I take the first 10 characters from the $rnd_id $rnd_id = substr($rnd_id,0,$random_id_length);
echo "Random Id: $rnd_id" ;
echo "<br>";
?>
4. Generate XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX style unique id, (8 letters)-(4 letters)-(4 letters)-(4 letters)-(12 letters)
<?php
// Generate Guid function NewGuid() {
$s = strtoupper(md5(uniqid(rand(),true)));
$guidText =
substr($s,0,8) . '-' .
substr($s,8,4) . '-' .
substr($s,12,4). '-' .
substr($s,16,4). '-' .
substr($s,20);
return $guidText;
}// End Generate Guid
$Guid = NewGuid();
echo $Guid;
echo "<br>";
?>
Sekian..
Belum ada tanggapan untuk "4 Cara Membuat auto uniq ID di php"
Post a Comment