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

  • Musta: Very good tutorial for beginners who want to get an idea of the ajax implemtation via jquery. Thanx
  • admin: Hello Mous, Kindly look at this link: http://www.ryantetek.com/2009/ 10/how-to-hack-psp-brite-30...
  • mous: Sir pls help.. i have a psp 3000 with firmware 5.02 i tried the instructions above and it wont work on me.. it...
  • milo: i think the quick solution is to have the pagination in ajax otherwise it wont work as expected im not sure...
  • admin: Hello Milo, I haven’t tried it with pagination before.Maybe I’ll take a look at it and post some...