<html><head></head><title></title>
<body onload="document.MyForm.users_number.focus()">
<?
// Solution of Assignment 3; CSIT 207 (Spring 07)
$users_numbers = $username_length = $password_length = 0;

echo <<< HERE
	<h2 align="center">Random Username and Password Generator</h2>
	<p>This program generates random distinct usernames of the given length, 
	   and random distinct passwords of the given length. The number of usernames
	   and passwords is given.
	</p>
	<form name="MyForm" action="{$_SERVER['PHP_SELF']}" method="post">
	<b>Enter the number of users (must be between 1 and 50): </b>
	<input name="users_number" type="text" size="3">
	<br />
	<b>Enter the length of the username (must be between 5 and 9): </b>
	<input name="username_length" type="text" size="3">
	<br />
	<b>Enter the length of the password (must be between 5 and 9): </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'];
	$username_length = $_POST['username_length'];
	$password_length = $_POST['password_length'];
	#echo $users_number, $username_length, $password_length, "<br>";
	if (($users_number >= 1 && $users_number <= 50)
		&& ($username_length >= 5 && $username_length <= 9)
		&& ($password_length >= 5 && $password_length <= 9))
	{
		$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;
		$b = array_merge(range('A','Z'),range('a','z'),range('0','9'));
		for ($i = 1; $i <= $users_number; $i++)
		{
			do
			{
				// Generate a random username consisting of letters and digits
			        $username = "";
        			for ($j=0; $j < $username_length; $j++)
	        		{
        	       			// Generate a random number n between 0 and 62
	                		$n = mt_rand(0,61);
			                // Get the element of $b whose position is $n
        			        $c = $b[$n];
                			// Concatenate this letter to the previous ones by
		        	        // using the dot operator.
        		        	$username = $username.$c;
        			}
			}
			while (in_array($username,$a));
	
			// Get the array of keys
			$keys = array_keys($a);
			do
			{
				// Generate a random password of the required length
				$password = mt_rand($r,$s);
			}
			while (in_array($password,$keys));
			#while (array_key_exists($password,$a)); // OK
	
			// Assign the usernames to the array and the passwords as keys
			$a[$password] = $username;
		}
		// Print the array
		echo "<b>Before Sorting: </b><br>\n";
		echo "<center><table width='40%' border='1'>\n<tr><td><b>Username</b></td><td><b>Password</b></td></tr>\n";
		foreach ($a as $k => $v)
			echo "<tr><td>$v</td><td>$k</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>Username</b></td><td><b>Password</b></td></tr>\n";
		foreach ($a as $k => $v)
        		echo "<tr><td>$v</td><td>$k</td></tr>\n";
		echo "</table></center>\n";
	}
	else
		echo "Check the length of the username and the password and the number of users.<br\>";
}
?>

</body>
</html>
