Generate a random password

Information for PHP. Hypertext Preprocessor
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

Generate a random password

Post by dzcadii » Sun Apr 05, 2015 6:52 am

Here is a simple way to generate a random password etc. Use at will/own risk, Just something to get you thinking.

Code: Select all

<?PHP
	function GeneratePassword($length = 10, $complex = 4)
	{
		$min = "abcdefghijklmnopqrstuvwxyz";
		$num = "0123456789";
		$maj = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$symb = "!@#$%^&*()_-=+;:,.?";
		$chars = $min;
			if($complex >= 2)
			{
				$chars .= $num;
			}
			if($complex >= 3)
			{
				$chars .= $maj;
			}
			if($complex >= 4)
			{
				$chars .= $symb;
			}
		$randomPassword = substr(str_shuffle($chars), 0, $length);
		return $randomPassword;
	}
	
	echo GeneratePassword(rand(10, 55));
?>

Post Reply