Return to Snippet

Revision: 19578
at October 26, 2009 10:11 by AzizLight


Initial Code
<?php
/**
* Generate a random password
*/
class Password
{
	/**
	 * Generate the new password
	 *
	 * @access public
	 * @param array $params
	 * @return string
	 **/
	public function generate($params = array())
	{
		
		$length     = (!array_key_exists('length', $params))     ? 15     :     $params['length'];
		$use_lower  = (!array_key_exists('use_lower', $params))  ? TRUE   :  $params['use_lower'];
		$use_upper  = (!array_key_exists('use_upper', $params))  ? TRUE   :  $params['use_upper'];
		$use_number = (!array_key_exists('use_number', $params)) ? TRUE   : $params['use_number'];
		$use_custom = (!array_key_exists('use_custom', $params)) ? '-_()' : $params['use_custom'];
		
		$upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$lower = "abcdefghijklmnopqrstuvwxyz";
		$number = "0123456789";
		
		$seed_length = 0;
		$seed        = '';
		
		if($use_upper === TRUE){
			$seed_length += 26;
			$seed .= $upper;
		}
		if($use_lower === TRUE){
			$seed_length += 26;
			$seed .= $lower;
		}
		if($use_number === TRUE){
			$seed_length += 10;
			$seed .= $number;
		}
		if(!empty($use_custom)){
			$seed_length +=strlen($use_custom);
			$seed .= $use_custom;
		}
		for($i = 1; $i <= $length; $i++){
			$password .= $seed{rand(0,$seed_length-1)};
		}
		return $password;
	} // End of generate
} // End of Class Password

?>

Initial URL

                                

Initial Description
I rewrote [this snippet](http://snipplr.com/view/15402/php-password-generator/).

Usage:
	
	$password1 = new Password;
	$password2 = new Password;
	
	echo '<p>' . $password1->generate() . '</p>';
	echo '<p>' . $password2->generate(array('length' => 50)) . '</p>';

Initial Title
Random Password Generator PHP Class

Initial Tags
class

Initial Language
PHP