Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php random id generator 8 char alpha numeric

Posted on 2009-07-03
10
Medium Priority
?
2,621 Views
Last Modified: 2012-05-07
Hi Guys,

I need to generate an 8 character alpha numeric string which I can use as a unique identifier for all new subscribers to a site.

Php experts, can help me with this?

Your help is very much appreciated.  : )

Thanks,
John
0
Comment
Question by:webgeek154
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 11

Expert Comment

by:BrianMM
ID: 24772122
I came up with this... it should create something with very slim chance of duplicates every few million! or has done for me!
$newid = md5rand(8);
 
function md5rand ($length) {
	$pass1 = strtoupper(md5(rand(0, 1000000)));
	$rand_start = rand(5,strlen($pass1));
	if($rand_start == strlen($pass1)) {
		$rand_start = 1;
	}
	$pass2 = strtoupper(substr(md5($pass1), $rand_start, $length));
$length));
	return $pass2;
}

Open in new window

0
 
LVL 4

Expert Comment

by:fsouzabrasil
ID: 24772133
add on array all alpha char and on array_rand insert number of indexes this array.
for
 $vlr = array("0", "1", "2", "3", "4", "5", "6", "7", "8","9","a","b","c","d","e","f","g");
$vlr0 = "";
for ($i = 1; $i <= 10; $i++) {
 $rand_num = array_rand($vlr, 17);
 $vlr0 += $rand_num[0];
}
echo $vlr0;

Open in new window

0
 
LVL 4

Expert Comment

by:fsouzabrasil
ID: 24772141
ops... subs for number 10 to number of alpha chars.
0
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
LVL 11

Expert Comment

by:BrianMM
ID: 24772145
here are a couple others i have too;
echo rand_str(8);
echo generateRandStr(8);
 
function rand_str($length = 32, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')
{
    // Length of character list
    $chars_length = (strlen($chars) - 1);
 
    // Start our string
    $string = $chars{rand(0, $chars_length)};
   
    // Generate random string
    for ($i = 1; $i < $length; $i = strlen($string))
    {
        // Grab a random character from our list
        $r = $chars{rand(0, $chars_length)};
       
        // Make sure the same two characters don't appear next to each other
        if ($r != $string{$i - 1}) $string .=  $r;
    }
   
    // Return the string
    return $string;
}
 
function generateRandStr($length){
      $randstr = "";
      for($i=0; $i<$length; $i++){
         $randnum = mt_rand(0,61);
         if($randnum < 10){
            $randstr .= chr($randnum+48);
         }else if($randnum < 36){
            $randstr .= chr($randnum+55);
         }else{
            $randstr .= chr($randnum+61);
         }
      }
      return $randstr;
   }

Open in new window

0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 24772220
You can prevent duplicates with this method
do{
        $usernumber=...//however you generate it...
        $result=mysql_query("Select * from $globalusertable where usernumber=$usernumber;");
}while(mysql_num_rows($result)>0);

Open in new window

0
 
LVL 7

Expert Comment

by:szewkam
ID: 24772429
or just use uniqid function from php ;)
http://php.net/manual/en/function.uniqid.php
On site you have examples of use :)
0
 
LVL 11

Accepted Solution

by:
BrianMM earned 2000 total points
ID: 24772657
the uniqueid function in php is'nt very unique :) depending on how you are using it it can generate the same id quite often. I have better functions, combine that with the idea from yodercm it's pretty foolproof.

Below are 3 other functions i generally use for UUIDs, I am quite fond of the one that prefixes the ID with something standard, then for a user you can prefis it with say 'U' then you know anything beginning with a U is a user and so on.
function generateRand_uuid ( $prefix = 'W' ) {
	// Perfect for: UNIQUE ID GENERATION
	// Create a UUID made of: PREFIX:TIMESTAMP:UUID
	$my_random_id = $prefix;
	$my_random_id .= chr ( rand ( 65, 90 ) );
	$my_random_id .= time ();
	$my_random_id .= uniqid ( $prefix );
	return $my_random_id;
}
 
function generateRand_uuid_l ( $prefix = 'W', $length=0 ) {
	// Perfect for: UNIQUE ID GENERATION
	// Create a UUID made of: PREFIX:TIMESTAMP:UUID(PART - LENGTH - or FULL)
	$my_random_id = $prefix;
	$my_random_id .= chr ( rand ( 65, 90 ) );
	$my_random_id .= time ();
	$my_uniqid = uniqid ( $prefix );
	if($length > 0) {
		$my_random_id .= substr($my_uniqid, $length);
	} else {
		$my_random_id .= $my_uniqid;
	}
	return $my_random_id;
}
 
function generateRand_md5uid(){
	// Perfect for: UNIQUE ID GENERATION
	// Create a really STRONG UUID
	// Very high odds of creating a unique string 1:1000000+
	$better_token = md5(uniqid(rand(), true));
	$unique_code = substr($better_token, 64);
	$uniqueid = $unique_code;
	return $better_token;
}

Open in new window

0
 

Author Closing Comment

by:webgeek154
ID: 31599550
thansk!
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 24773018
Despite ignoring my post, please remember that even the functions given by BrianMM are able to produce non-unique values.  Please use them within the loop method I gave you, to ENSURE uniqueness.
0
 

Author Comment

by:webgeek154
ID: 24773052
Hi Yoder,

Sorry about that.  What happened is that a colleague of mine handed me the code i needed right after submitting the post.  Then i started seeing responses so I came back and closed it as fast as possible so I didn't waste more of people's time.

But thanks.  I really do appreciate your help.  Really.

Thanks,
John
0

Featured Post

[Webinar] Protection from Cyberattacks

In this session, we’ll dive into the complexities of modern cyber threats and why only multi-vector protection can keep today’s businesses secure through the various stages of a cyberattack, across multiple vectors. Thursday September 14, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question