logo  
 
 
 
 
 
 
 
     
   

Handout #4 - CSCI 2442

01 - Create a directory under htdocs called handout4. You will manually create four files in this directory called index.htm, page2.php, manage.php and write.php. In addition, the process of running these scripts will create the following text files: blacklist.txt, iplist.txt, quotes.txt and timelog.txt.

02 - The file index.htm will be a simple html file containing a form. The form action will submit the results to page2.php using the post method and will contain three radio buttons plus a submit button. Each radio button will have the name "selection". The first radio button will have a value of "read", the second will have a value of "write" while the third will have a value of "manage". The read button should be checked by default. Next to each button, place the following text strings: "Read the Quotes File", "Write to the Quotes File" and "Manage the Quotes File". Apply various background colors, font colors and font sizes as desired.

03 - The page2.php file should contain the same colors and styles used in the index.htm file. Inside the php section of the page2.php file, create a variable called selection and set it equal to the value passed to it from index.htm. The next section will contain an if/else structure, testing three cases. If the value of selection is set to "read", the quotes.txt file will be read as described in Step 4. If selection is set to "write", the quotes file will be written to as described in Step 5. If selection is set to "manage, the blacklist.txt file will be modified as described in Step 6. Please include php comments for each of these sections. After the php script is closed, please add closing body and html tags.

04 - If the read option is selected, test if the quotes.txt file currently exists. If it does NOT exist, print out a message stating the quotes file is currently empty followed by a clickable link referring the user back to index.htm. If it does exist, set the quotes.txt file equal to an array called input1 using the file() function. Insert the message "The entire quotes file is displayed below" then create a div block with different colors and fonts than those used in the body tag. Using a loop structure, display every element of the $input1 array inside this div block. Outside of the loop, close the div tag then create a clickable link referring the user back to index.htm.

05 - The beginning of the "write" section should first touch the file blacklist.txt and then define four new variables: blacklisted, client_ip, datestamp and input2. The variable blacklisted should be set to FALSE, client_ip should be set to the REMOTE_ADDR of the clients computer, datestamp should get the current date on the server in the format "Y-m-d-H-i-s" while input2 should be set to the file blacklist.txt as an array. Using a loop structure, test if any of the values in the array input2 are equal to the variable client_ip. If they are, set the variable blacklisted to TRUE. Once the loop is finished, create an if statement testing if the variable blacklisted. If blacklisted is TRUE, print the message "You are not allowed to write to the quotes file" followed by a link back to index.htm . If blacklisted is FALSE, display a form with the action pointing to write.php using the post method. Inside this form, insert the message "Enter your quote" along with a text input field called "quote1" with a size to 100. Follow this with 2 hidden input fields called "ip_address" and "datestamp" which contain the variables client_ip and datestamp. Include a submit button them close the form tag.

06 - For the manage quotes section, create three variables called input1, input2 and input3. Use the file() function to set them equal to the files quotes.txt, iplist.txt and timelog.txt. Display the message "Leave all checkboxes blank if you do not wish to block any IP addresses". Next, insert a form tag with the action pointing to manage.php and the method of post. After this insert a table. The first row will contain four data cells containing the labels "Quote", "Address", "Time" and "Block IP Address?". Next create a loop structure to go through every value of input1. Inside the loop, create a variable called boxid which is equal to the word "box" concatenated with the current numerical value of the key. (For the first pass, boxisshould equal "box0", then "box1", then "box2", etc) Next, inside the loop, write a table row, followed by four table cells, then close the table row tag. The first cell should contain the element for input1[$key], the second should contain the element for input2[$key], the third should contain the element for input3[$key] while the fourth is a radio button. The radio button should have the name "badbox" and the value should be set to "$boxid". Outside of the loop, close the table tag, then display the message "Quote management requires administrative privileges. Please login." Insert a text and password input field called username and password, followed by a submit button. Close the form tag. This should complete this section.

07 - Create a new file called write.php with the same styles used in index.htm. Inside the php script section, create three variables called quote1, ip_address and timestamp. Set these equal to the values passed to it from Step 5. Next create three variables called fp1, fp2 and fp3 and use the fopen function to set them equal to quotes.txt, iplist.txt and timelog.txt. Use the 'a' option inside each fopen() function. Next, write the variable quote1 to fp1, ip_address to fp2 and datestamp to fp3 using the fwrite() function. Make sure to include a newline character after each variable. Use the fclose() function for the variables fp1, fp2 and fp3, then display the message "Your quote has been written." Follow this by a link to return to the main index page, then write closing body and html tags.

08 - Create a new file called manage.php with the same styles used in index.htm. Inside the php script section, create four variables called username, password, badbox and badindex. The first three should be set to the values passed from step 6. Use the str_replace() function to strip out just the numerical portion of badbox and set it equal to badindex. Next, create a if structure testing if username is equal to "admin1" AND password is equal to "qwerty". If this is not true, print the message "The username and/or password is incorrect" followed by a link directing the user back to index.htm. If the statement is true, create another if structure testing if badindex contains a value or not. If badindex does not contain a value, this means no radio button was selected in Step 6. For this case, display the message "No IP Address was selected" followed by a link to index.htm. Otherwise, define two variables called input1 and badaddress. Set input1 equal to the file iplist.txt, then set badaddress equal to the value found in input1[$badindex]. Next, use the fopen function to open the file blacklist.txt with the append option and set it equal to a variable called fp4. Use the fwrite command to write the variable badaddress to fp4 and then use fclose on fp4. Make sure to use the newline character when writing badaddress. Follow this with the statements "The IP address $badaddress can no longer write to the quotes file." followed by the message "The previous quotes submitted by $badaddress will still be displayed however". Include a link back to the index.htm file then close the body and html tags.

09 - To test the handout4 project, first verify the the .txt files blacklist, iplist, quotes and timelog do not exist in the handout4 directory. Using the URL 127.0.0.1/handout4/, select the write option two times and enter two unique quotes. Run the ipconfig command demonstrated in class to determine your current IP address. Using the URL YOUR_IP_ADDRESS/handout4/, select the write option and enter two more unique quotes. Select the read quotes option next and print the browser results. Next select the manage quotes option and choose to block the first box containing the IP address 127.0.0.1. Enter admin1 for the username and x as the password. Print the browser results. Select the manage option again and choose to block the first box containing the IP address 127.0.0.1. Enter admin1 for the username and qwerty as the password. Print the browser results. Using the URL 127.0.0.1/handout4/, attempt to write another quote. Print the browser results. Turn in the four printouts plus the local source code for page2.php, write.php and manage.php. Be prepared to demonstrate the results in class.

A working example of this code can be found HERE

BACK