Jump to content

is it this function got bug?


zgkhoo

Recommended Posts

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..

 

 

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.