HomeCoding & Programming

Generate unique n-digit random alphanumeric codes?

Generate unique n-digit random alphanumeric codes?
Like Tweet Pin it Share Share Email

Syntax

uniqid (prefix, more_entropy);

 

A unique website ID can be created in PHP using the uniqid () function.

This function has two parameters that can be set.

 

prefix

This is what will be appended to the beginning of each ID.

 

more_entropy

If this is false or not specified it will return 13 characters, if it is true then 23 characters will be returned.

 

Below are some examples:


<?php

/*creates a unique id with the 'sa' prefix*/
$a = uniqid(sa); echo $a; echo "<br />";

/*creates a longer unique id with the 'sa' prefix*/
$b = uniqid (sa, true); echo $b; echo "<br />";

/*creates a unique ID with a random number as a prefix - this is 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;

?>

Comments (6)

  • Your style is really unique in comparison to other folks I have read stuff from.
    Thank you for posting when you’ve got the opportunity, Guess I will just bookmark this site.

  • Wow, marvelous blog structure! How long have you ever been
    running a blog for? you make blogging glance easy. The entire look of your web site is magnificent, as well as the
    content!

  • Great beat ! I would like to apprentice while you amend your site,
    how could i subscribe for a blog website? The account aided me a acceptable deal.
    I had been tiny bit acquainted of this your broadcast offered bright clear concept

  • Very nice post. I just stumbled upon your weblog and wished to say that.
    I’ve truly enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!

  • It’s great that you are getting ideas from this paragraph as well as from our argument made here.

  • This article is truly a nice one it helps new net users,
    who are wishing in favor of blogging.

Comments are closed.