Random string(password) in php function


stáhnout snippet
zobrazení: SyntaxHighlighter | GeSHi | Holý text
  /**
   * Random string(password) generation
   *
   * @param integer $length
   * @return string
   */
  public static function generatePassword($length = 8)
	{
	
	  // start with a blank password
	  $password = "";
	
	  // define possible characters
	  // avoiding y and z for confusing czech/english keyboards, 1 and l, 0 and O
	  $possible = "23456789abcdefghijkmnopqrstuvwx";  
	  
	  // 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++;
	    }
	
	  }
	
	  // done!
	  return $password;
	
	}


Tagy:
php 37 řádků | 2008-10-03 03:46:28 | air.kadlec@seznam.cz