<html><body onLoad="document.MyForm.Length.focus()">

<!-- Solution 1 of Quiz 1 

(1) Form an array $a whose elements are the lowercase letters, 
	uppercase letters, and numbers 0 to 9, by merging the three 
	arrays below. Here is how:

		$a1 = range('a','z');
                $a2 = range('A','Z');
                $a3 = range('0','9');
                $a = array_merge($a1,$a2,$a3);

(2) Get a random number between 0 and 61 using mt_rand(0,61). Here is how:

	 $k = mt_rand(0,61);

(3) Get the element of $a that has that random row and random column indices.

        $n = $a[$k];

(4) Now concatenate the new character to the previous ones

                                $pass .= $n;

-->

<?php

echo <<< Begin

<center>

<table width=50%>
<tr><td><center>

<fieldset>
<B><legend>Password Generator</legend></B>
<FORM ACTION="{$_SERVER['PHP_SELF']}" METHOD="POST" name="MyForm">

Enter the length of the password: 
<input name="Length" size=30>
<P>

Enter the number of passwords:
<input name="Number" size=30>
<P>
<input name="submitted" TYPE="hidden" value="true">
<P>
<input type="submit" VALUE="SUBMIT"> 
<input type="reset" VALUR="RESET">
</form>
</fieldset>

Begin;

if (isset($_POST['submitted'])) // If the form is submitted
{
        $length = (integer) trim($_POST['Length']);
        $number = (integer) trim($_POST['Number']);
        echo "You requested: $number passwords each of length $length.\n<P>";
        if ($length > 0 && $number > 0)
        {
		$a1 = range('a','z');
		$a2 = range('A','Z');
		$a3 = range('0','9');
		$a = array_merge($a1,$a2,$a3);
		echo "<B>Here they are:</B><P>\n";
                for ($i = 0; $i < $number; $i++)
                {
                        $pass = "";
                        for ($j=0; $j < $length; $j++)
                        {
				$k = mt_rand(0,61);
                                $n = $a[$k];
                                $pass .= $n;
                        }
                        echo "<P>$pass\n";
                }
        }
        else
                echo "<P><B> You have to enter positive integers.<P>\n";
	echo "</center></td></tr></table></center>\n";
}

?>
</body></html>
