1.13 Creating a Unique Identifier


You want to create a unique identifier; for example, for a session ID or a product ID in the database.

Technique

You can use uniqid() , getmypid() , md5() , and microtime () to generate a random ID:

 <?php $id =  md5(uniqid(microtime(), 1)) . getmypid(); ?> 

Comments

In this example, the getmypid() function is optional ”it's there to make sure that IDs generated by different server processes are not the same. But because md5() here depends on uniqid() , which in turn depends on microtime() , there is a very, very slim chance of ever generating the same ID.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net