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

<!-- Solution 4 of Quiz 1 

(1) Get a random number $k from 0 to 2 (0 for numbers 0 to 9, 1 for uppercase letters, 2 for lowercase 
	letters). Here is how:

	$k = mt_rand(0,2);

(2) Get a random ASCII position $m for a number (0 to 9), uppercase letter or lowercase letter based on the 
	random number $k you got above. Here is how:

                                switch ($k)
                                {
                                  case 0:
                                        $m = rand(48,57);
                                        break;
                                  case 1:
                                        $m = rand(65,90);
                                        break;
                                  case 2:
                                        $m = rand(97,122);
                                        break;
                                }


(3) Get the character $n that has the ASCII position $m. Here is how:

	$n = chr($m);

(5) 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)
        {

		echo "<B>Here they are:</B><P>\n";
                for ($i = 0; $i < $number; $i++)
                {
                        $pass = "";
                        for ($j=0; $j < $length; $j++)
                        {
				// Now get a random numner between 0 and 2
				$k = mt_rand(0,2);

				// Now get a random acceptable ASCII position
				switch ($k)
				{
				  case 0:
					$m = rand(48,57);
					break;
				  case 1:
                                        $m = rand(65,90);
                                        break;
				  case 2:
                                        $m = rand(97,122);
                                        break;
				}
				
				$n = chr($m);

				// Now concatenate the new character to the previous ones.
                                $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>
