note 30789 added to function.uniqid
| From: | father at bigattichouse dot com | Date: | Fri, 28 Mar 2003 15:11:57 +0000 |
| Subject: | note 30789 added to function.uniqid | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-45955@lists.php.net to get a copy of this message | ||
Out of frustration I created the following set of code to create base64 encoded 24byte GUIDs that
might be compatible with the base64 encoded GUIDs I use in windows... I went a little crazy on the
seeds, but it seems to work. I'll have to check and see how long it takes to actually run this
code... but, on the bright-side, it makes a MUCH smaller key than the 38-40char keys I'd use
with md5 alone.
I used some of the techniques mentioned in the other notes for uniqid(), see the unit test
(phptest_Expand_eq_Compress) at bottom for proof on how it works.
function CreateGUID(){
srand((double)microtime()*1000000);
$r = rand ;
$u = uniqid(getmypid() . $r . (double)microtime()*1000000,1);
$m = md5 ($u);
return($m);
}
function CompressID( $ID ){
return(Base64_encode(pack("H*",$ID)));
}
function ExpandID ($ID){
return ( implode(unpack("H*",Base64_decode($ID)), '') );
}
function phptest_Expand_eq_Compress(){
$ID=CreateGUID();
$CID = CompressID($ID) ;
$EID = ExpandID($CID);
echo $CID . "<BR>" . $EID . "<BR>";
if (strlen($CID)==24){echo "PASSED";} else {echo "FAILED";}
echo ":24 byte GUID.<BR>";
if ($ID==$EID){echo "PASSED";} else {echo "FAILED";}
echo ":24 byte GUID expand=compressed.<BR>";
}
**********
So to hand a key back to a browser use:
$ID=CreateGUID();
echo '<A HREF="customer.php?CUSTOMERID=' . $ID .
'">Bill</A>";
When storing (or reading from the DB):
$CID=CompressID($CUSTOMERID);
$SQL="Select * from Customers where CustomerID='$CID'";
You get the Idea.. this should shave a LOT from your GUID-oriented primary keys.
Mike Johnson
http://www.bigattichouse.com/
--
http://www.php.net/manual/en/function.uniqid.php
http://master.php.net/manage/user-notes.php?action=edit+30789
http://master.php.net/manage/user-notes.php?action=delete+30789
http://master.php.net/manage/user-notes.php?action=reject+30789