Jump to content

PHP random code generator


kanala

Recommended Posts

I need a PHP random code generator which generates a unique 6 digit code (of letters and numbers).I found some on Google but they didn't work.Thanks :)

Link to comment
Share on other sites

This was justsomeguy's code from another topic. Just change whatever fits your needs.

############################################################ generate_password#  this function generates a random alphanumeric password###########################################################function generate_password ($length = 8){  $user_pw = "";  while (strlen($user_pw) < $length)  {	$char = mt_rand(49, 122);	if (($char > 57 && $char < 65) ||		($char > 90 && $char < 97))	{	  continue;	}	$user_pw .= chr($char);  }  return $user_pw;}

Link to comment
Share on other sites

the code the user above me posted should work. If you want an alternative, i usually do something(kinda random) like this:

function generate_rand_string($length=10){	 return substr(md5(uniqid().(time()+microtime() ) ),0,$length );}//Example usage$string = generate_rand_string(6);//$string would now be one random string.

Link to comment
Share on other sites

Yeah that will work, but only for strings <= 32 characters.
I know. Because of the md5 all output will be 32 characters long however, Do you really need a random string more than 32 characters? I have yet to find a case that i do.
Link to comment
Share on other sites

I know. Because of the md5 all output will be 32 characters long however, Do you really need a random string more than 32 characters? I have yet to find a case that i do.
No, its only a small length code of about 8 letters or digits. Thanks for the code, I'll try it out.
Link to comment
Share on other sites

Out of curiosity I decided to benchmark the functions, I was thinking that generate_password would be better for smaller strings because it uses more lightweight function calls, but it's also in a loop so generate_rand_string would be quicker for larger passwords because it only calls each function once, even though they are more heavyweight functions. I wasn't sure what the cutoff was though, how many characters it had to be for the one function to be faster. So I put them in a loop to run 100,000 times each and timed them, and also included a sha1 version and an md5 version. Here are the times, in seconds:

generate_password(4): 1.83689808846generate_password(6): 2.43429493904generate_password(8): 3.21072697639generate_password(10): 3.94861698151generate_password(16): 6.12373304367generate_password(24): 8.95758104324generate_rand_string_md5(4): 2.29813814163generate_rand_string_md5(6): 2.29769897461generate_rand_string_md5(8): 2.31600308418generate_rand_string_md5(10): 2.31662893295generate_rand_string_md5(16): 2.31204390526generate_rand_string_md5(24): 2.29829597473generate_rand_string_sha1(4): 2.51115489006generate_rand_string_sha1(6): 2.4965429306generate_rand_string_sha1(8): 2.5090470314generate_rand_string_sha1(10): 2.50350117683generate_rand_string_sha1(16): 2.50540590286generate_rand_string_sha1(24): 2.50517988205

So generate_password is only faster for strings smaller then 5 or 6 characters, and md5 is faster then sha1.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...