Search Keyword:

07Jul

Create a random string/password generator - PHP

Posted by admin as PHP

This PHP function creates a random string that can be used as a password generation tool for your website.You can generate an alphabetic,numeric,alphanumeric or alphanumeric with symbols random string that can be used as a random password.Just modify the array in the code to which characters you will use in the string.The string length can be specified in the parameter.If no parameter has been st,default will be 8.Just call the function and specify the length in the parameter(optional) then you have a random string generated.

function random_generator($digits){
if ($digits==”")
{
$digits=8; //specify default length if empty
}
srand ((double) microtime() * 10000000);
//Array of alphabets
$input = array (”a”, “b”, “c”, “d”, “e”,”f”,”g”,”h”,”i”,”j”,”k”,”l”,”m”,”n”,”o”,”p”,”q”,
“r”,”s”,”t”,”u”,”v”,”w”,”x”,”y”,”z”,”A”, “B”, “C”, “D”, “E”,”F”,”G”,”H”,”I”,”J”,”K”,”L”,”M”,”N”,”O”,”P”,”Q”,”R”,
“S”,”T”,”U”,”V”,”W”,”X”,”Y”,”Z”);

$random_generator=”";// Initialize the string to store random numbers
for($i=1;$i<$digits+1;$i++){ // Loop the number of times of required digits

if(rand(1,2) == 1){// to decide the digit should be numeric or alphabet
// Add one random alphabet
$rand_index = array_rand($input);
$random_generator .=$input[$rand_index]; // One char is added

}else{

// Add one numeric digit between 1 and 10
$random_generator .=rand(1,10); // one number is added
} // end of if else

} // end of for loop

return $random_generator;
}

echo random_generator(10); //call the function

  • Digg
  • del.icio.us
  • Tumblr
  • StumbleUpon
  • Technorati
  • Reddit
  • NewsVine
  • Slashdot
  • DZone
  • Facebook
  • Twitter

Comment Form

Sponsored Links



Recent Comments

  • admin: Hello Ron, You can check the firmware by navigating into Settings > System Settings > System Information .The...
  • Ron: I Just Bought A PSP 3000 And The Mfr. Date Said 11/09/09 And Was Wondering If Anyone Know What Firmware Is On...
  • MANEE323: Took me awhile but I finally got it THANKS a million!!!!!!
  • spencer bandin: ang galing mo po kuya ryan…
  • chris: Thanks,ill give it a go.