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

<!-- Solution 3 of Quiz 1 
(Using array_combine, array_merge, and array_rand)

(1) Form an array whose elements are the indices of the smallcase letters 
	(using range(97,122)), uppercase letters (using range(65,90)), 
	and the numbers 0 to 9 (using range(48,57)). Merge the three range arrays 
	into one array using the array_merge function. Call this array $d. The indices 
	of $d will be 0 to 61. Here is how to do that:

	$d = array_merge(range(48,57),range(65,90),range(97,122));

(2) Now change the indices of $d to make them the same as the elements themselves using 
	
	$d = array_combine($d,$d);

(3) Now get a random index from $d using array_rand.
	
	$k = array_rand($d);

(4) Get the character whose index is that in the ASCCII table by using the chr function.

	$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)
        {
		// To create an array who indices are the same as the values
		// which are 48 through 57:
		 // You can use this method
		/* $a = range(48,57);
		$a1 = array();
		// Now make the indices the same as the values:
		for ($i = 48; $i <= 57; $i++)
			$a1[$i] = $i;
		*/
		// or this one
		/* $a1 = array_combine(range(48,57),range(48,57));
		// print_r($a1); echo "<BR>";
		*/
	
		/* Now create an array whose elements are the indices in the ASCII
			table of the numbers 0..9, smallcase and uppercase letters.
			The indices of the elements will be 0 through 61
		*/

		$d = array_merge(range(48,57),range(65,90),range(97,122));

		/* Now change the indices to be 48 through 57, 65 through 90, 
			and 97 through 122.
		*/
 
		$d = array_combine($d,$d);

		// print_r($d);
		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 index
				$k = array_rand($d);

				// Now get the character that has that index
                                $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>
