Science, Tech, Math › Computer Science How to Generate a Unique ID in PHP Share Flipboard Email Print Scott-Cartwright/Getty Images Computer Science PHP Programming Tutorials MySQL Commands Perl Python Java Programming Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Angela Bradley Angela Bradley Computer Science Expert B.A, History, Eastern Oregon University Angela Bradley is a web designer and programming expert with over 15 years of experience. An expert in iOS software design and development, she specializes in building technical hybrid platforms. Learn about our Editorial Process Updated on October 02, 2018 A unique user ID can be created in PHP using the uniqid () function. This function has two parameters you can set. The first is the prefix, which is what will be appended to the beginning of each ID. The second is more_entropy. If this is false or not specified, it will return 13 characters; if it's true, 23 characters will be returned. Examples For Creating a Unique ID Below are examples of creating a unique user ID, but each are a little different. The first creates a normal unique ID while the second shows how to make a longer ID. The third example creates an ID with a random number as the prefix while the last line can be used to encrypt the username before storing it. //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; ?> Cite this Article Format mla apa chicago Your Citation Bradley, Angela. "How to Generate a Unique ID in PHP." ThoughtCo, Feb. 16, 2021, thoughtco.com/how-to-generate-unique-id-2694169. Bradley, Angela. (2021, February 16). How to Generate a Unique ID in PHP. Retrieved from https://www.thoughtco.com/how-to-generate-unique-id-2694169 Bradley, Angela. "How to Generate a Unique ID in PHP." ThoughtCo. https://www.thoughtco.com/how-to-generate-unique-id-2694169 (accessed June 8, 2023). copy citation