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: Thanks Tony for the share!
  • Tony Stark: Hi Admin, Thanks for the tutorial. Chicken is too slow so I Google for an alternative and I found...
  • admin: How about installing Gen-B instead of Gen-C? Here’s the download link for it:...
  • aznspryd: I hacked my PSP-3000 and installed Gen-C, but everytime i try opening a game, it restarts the psp
  • vinoth: Hi ,bro its realy workin well i.E thanks alot.