<html><head></head><title></title>
<body onload="document.MyForm.users_number.focus()">
<?
$users_number = 0;
$password_length = 0;
echo <<< HERE
	<h2 align="center">Random Password Generator</h2>
	<p>This program generates random distinct passwords of the given length. The number of the passwords is 
	given.
	</p>
	<form name="MyForm" action="{$_SERVER['PHP_SELF']}" method="post">
	<b>Enter the number of users (must be between 1 and 10): </b>
	<input name="users_number" type="text" size="3">
	<br />
	<b>Enter the length of the password (must be between 3 and 7): </b>
	<input name="password_length" type="text" size="3">
	<br />
	</b>
	<input type="hidden" name="submitted" value="true">
	<input type="submit" value="SUBMIT"> <input type="reset" value="RESET">
</form>
<hr/>

HERE;

if (isset($_POST['submitted']))
{
	$users_number = $_POST['users_number'];
	$password_length = $_POST['password_length'];
	if (($users_number >= 1 && $users_number <= 10)
		&& ($password_length >= 3 && $password_length <= 7))
	{
		$a = array();
		// Find the lower limit for the password
		$r = pow(10,$password_length - 1);
		// Find the upper limit for the password
                $s = pow(10,$password_length) - 1;
		for ($i = 1; $i <= $users_number; $i++)
		{
			do
			{
				$password = mt_rand($r,$s);
			}
			while (in_array($password,$a));
			$a[] = $password;
		}
		// Print the array
		echo "<b>Before Sorting: </b><br>\n";
		echo "<center><table width='40%' border='1'>\n<tr><td><b>Password</b></td></tr>\n";
		foreach ($a as $v)
			echo "<tr><td>$v</td></tr>\n";
		echo "</table></center>\n";

		// Sort the array
		asort($a);

		// Print the array after sorting
		echo "<b>After Sorting: </b><br>\n";
		echo "<center><table width='40%' border='1'>\n<tr><td><b>Password</b></td></tr>\n";
                foreach ($a as $v)
                        echo "<tr><td>$v</td></tr>\n";
                echo "</table></center>\n";
	}
	else
		echo "Check the length of the password and the number of users.<br\>";
}
?>

</body>
</html>
