zgkhoo Posted November 2, 2007 Share Posted November 2, 2007 function generatePassword ($length,$type) { $password = ""; if ($type=='Character(Big Letter)'){//3 $possible="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } else{//3 if($type=='Character(Small Letter)'){//2 $possible="abcdefghijklmnopqrstuvwxyz"; }//2 else{//2 if ($type=='Number'){//1 $possible="0123456789"; }//1 else{//1 $possible = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; }//1 }//2 }//3 // set up a counter $i = 0; // add random characters to $password until $length is reached while ($i < $length) { // pick a random character from the possible ones $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); // we don't want this character if it's already in the password if (!strstr($password, $char)) { $password .= $char; $i++; } }//end of while // done! return $password; } i always hang with pages running this function...is it contain bugs? thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/75792-is-it-this-function-got-bug/ Share on other sites More sharing options...
Psycho Posted November 2, 2007 Share Posted November 2, 2007 Ever hear of the switch operator? Those nested IF statements may be valid but are not efficient. Anyway, I didn't run the code, but one problem I see is that you don't allow repeating characters. So, if you were to select the 'Number' type with a length greater than 10 the function would never complete. Try this (not tested) <?php function generatePassword ($length, $type) { switch ($type) { case 'Character(Big Letter)': $possible="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; break; case 'Character(Small Letter)': $possible="abcdefghijklmnopqrstuvwxyz"; break; case 'Number': $possible="0123456789"; break; default: $possible="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; break; } //Validation needed if characters cannot be repeated if ($length < strlen($possible)) { $length = strlen($possible); } //Convert string into an array of the characters $charactersAry = str_split($possible); //Randomize the order of the characters shuffle($charactersAry); //Get the first ($length) characters of the array and onvert to string $password = implode('', array_slice($input, 0, $length)); return $password; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75792-is-it-this-function-got-bug/#findComment-383599 Share on other sites More sharing options...
rajivgonsalves Posted November 2, 2007 Share Posted November 2, 2007 well as I told you before if number is selected it will hang $possible="0123456789"; Quote Link to comment https://forums.phpfreaks.com/topic/75792-is-it-this-function-got-bug/#findComment-383607 Share on other sites More sharing options...
Psycho Posted November 2, 2007 Share Posted November 2, 2007 There were a couple typos in my code above, but here it is corrected and it works perfectly fine. I included two different validations on length, be sure to comment out/remove the one you don't want to use. The function works perfectly fine for $possible="0123456789"; <?php function generatePassword ($length=6, $type='') { switch ($type) { case 'Character(Big Letter)': $possible="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; break; case 'Character(Small Letter)': $possible="abcdefghijklmnopqrstuvwxyz"; break; case 'Number': $possible="0123456789"; break; default: $possible="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; break; } //*******************************************// // USE ONLY ONE OF THE FOLLOWING VALIDATIONS // //*******************************************// //Use this validation if you want to reduce the length if greater than the //possible characters, i.e. characters must not repeat if ($length > strlen($possible)) { $length = strlen($possible); } //Use this validation if you want to allow dupluicate characters if the length //can be greater than the possible characters while ($length > strlen($possible)) { $possible = $possible . $possible; } //Convert string into an array of the characters $charactersAry = str_split($possible); //Randomize the order of the characters shuffle($charactersAry); //Get the first ($length) characters of the array and onvert to string $password = implode('', array_slice($charactersAry, 0, $length)); return $password; } echo "Big: " .generatePassword (12, 'Character(Big Letter)') . "<br>"; echo "Small: " .generatePassword (12, 'Character(Small Letter)') . "<br>"; echo "Number: " .generatePassword (12, 'Number') . "<br>"; echo "Default: " .generatePassword (12, 'xyz') . "<br>"; ?> Possible output if using the 2nd validation technique: Big: SLMAPWKIRZCO Small: keitqwmnghay Number: 909213427088 Default: y58uloQHEpwm Note: The 'Number' example repeats some characters because the length is specified as 12, but there are only 10 digits. Quote Link to comment https://forums.phpfreaks.com/topic/75792-is-it-this-function-got-bug/#findComment-383704 Share on other sites More sharing options...
zgkhoo Posted November 3, 2007 Author Share Posted November 3, 2007 after i try then let u know. thanks... Quote Link to comment https://forums.phpfreaks.com/topic/75792-is-it-this-function-got-bug/#findComment-384116 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.