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

<!-- Solution 5 of Quiz 1 

(1) Get a random number $k between 48 and 122. Here is how

	$k = mt_rand(48,122);

(2) Now note that the numbers 58 to 64 and 91 to 96 are not 
	indices of letters or numbers, so if you get such a number, keep 
	trying until you don't get a number from them. Here is how to do that
 
	while (in_array($k,$not_allowed))
              $k = mt_rand(48,122);


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

	$n = chr($k);

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


		$not_allowed = array_merge(range(58,64),range(91,96));

		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 number between 48 and 122
				$k = mt_rand(48,122);
							
				/* If the random number you got is not an ASCII position 
					for a letter or a number, keep trying until you 
					get an ASCII position of a numner or a letter
				*/
				while (in_array($k,$not_allowed))
					$k = mt_rand(48,122);
				
				// Now get the character whose ASCII position is $k
				$n = chr($k);

				// 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>
