note 30789 deleted from function.uniqid by aidan

From: Date: Sun, 07 Nov 2004 11:52:24 +0000
Subject: note 30789 deleted from function.uniqid by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80224@lists.php.net to get a copy of this message
Note Submitter: father at bigattichouse dot com ---- 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/

« previous php.notes (#80224) next »