Random key generator

Generates a random key, a-Z 0-9 with no max length

<?php
function keygen($length=10)
{
	$key = '';
	list($usec, $sec) = explode(' ', microtime());
	mt_srand((float) $sec + ((float) $usec * 100000));
	
   	$inputs = array_merge(range('z','a'),range(0,9),range('A','Z'));

   	for($i=0; $i<$length; $i++)
	{
   	    $key .= $inputs{mt_rand(0,61)};
	}
	return $key;
}


// echo keygen(40);  // JVP8xGk4hsX2cZd0L3NQwYbI0mf4exPiSoAhVYnz , your results will vary
?>

Usage

Use as demonstrated in the comment. This can be good to append to a url and store in a database, and check for in a site registration during email validation (i.e. http://site.com/register.php?connfirmationcode=2QwYbI0mf4exPi


Comments

guest
Posted on 31.07.2012 16:50

Tahnks !!

guest
Posted on 10.12.2011 07:17

hi, guest your code is awesome than the original one.its user friendly and we can change our key characters easily by editing $options. i prefer your code than the original one posted.

guest
Posted on 26.10.2011 01:25

Nice function guys. I will be using it to write a function for generating card pins for web login. Pins generated are stored on a database. When a pin is generated it is matched with the ones on the database if it exits, it is discarded if not exit, it is added to the database.

What do you think?

guest
Posted on 28.03.2011 10:23

What about uniqueness?

guest
Posted on 19.10.2010 18:28

fuck

guest
Posted on 09.08.2010 10:57

nice, i use something similar
<?php
function generatekey($length)
{
$options = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$code = "";
for($i = 0; $i < $length; $i++)
{
$key = rand(0, strlen($options) - 1);
$code .= $options[$key];
}

return $code;
}
// echo generatekey(10); // Ko9B69utve, returned result is random. not only is this good for ac validatin but has several other applicvations, eg captch ect its uses are limitless.
?>

Add your comment