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

  • anup: thanx it helped me alot…
  • admin: try to look at window.location function of Javascript and place it under the success function of the AJAX...
  • ana: it works for me, but after success, how can i forward the url to a certain page? thanks
  • admin: Hello Don, do these steps again and again until the PSP reboots. Restore your PSP’s Default settings in SYstem...
  • don sb: my psp was already hacked then i changed the screen cos it broke,so at one point the battery lost total...