1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5/MySQL Programming- P49 pps

5 152 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 80,45 KB

Nội dung

function buildHTML(){ global $quizName, $quizData; $htData = <<<HERE <html> <head> <title>$quizName</title> </head> <body> HERE; //get the quiz data $problems = split(“\n”, $quizData); $htData .= <<<HERE <center> <h1>$quizName</h1> </center> <form action = “gradeQuiz.php” method = “post”> Name <input type = “text” name = “student”> <ol> HERE; $questionNumber = 1; foreach ($problems as $currentProblem){ list($question, $answerA, $answerB, $answerC, $answerD, $correct) = split (“:”, $currentProblem); $htData .= <<<HERE <li> $question <ol type = “A”> <li> <input type = “radio” name = “quest[$questionNumber]” 218 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r value = “A”> $answerA </li> <li> <input type = “radio” name = “quest[$questionNumber]” value = “B”> $answerB </li> <li> <input type = “radio” name = “quest[$questionNumber]” value = “C”> $answerC </li> <li> <input type = “radio” name = “quest[$questionNumber]” value = “D”> $answerD </li> </ol> </li> HERE; $questionNumber++; } // end foreach $htData .= <<<HERE </ol> <input type = “hidden” name = “quizName” value = “$quizName”> <input type = “submit” value = “submit quiz”> 219 C h a p t e r 6 W o r k i n g w i t h F i l e s </form> HERE; print $htData; return $htData; } // end buildHTML ?> </body> </html> Most of the critical information this function needs is stored in $quizData. Each line of $quizData stores one question, so I use a split() function to break $quizData into an array called $problems. A foreach loop steps through each problem. Each problem contains a list of values, which is separated into a series of scalar vari- ables with the combination of split() and list(). Within the foreach loop, I also add the HTML code necessary to print the current question’s information. Take a careful look at the code for the radio buttons. Recall from your HTML experience or Appendix A that radio buttons that operate as a group should all have the same name. I did this by calling them all quest[$questionNumber]. The $questionNumber variable contains the current ques- tion number, and this value is interpolated before the HTML code is written. Question number 1 has four different radio buttons called quest[1]. The gradeQuiz program sees this as an array called $quest. At the end of the HTML, I add the quiz name (as a hidden field) and the submit button. Taking a Quiz The point of all this work is to have a set of quizzes your users can take, so it’s good to have a program to present the quizzes. Actually, since the quizzes are saved as HTML pages, you could simply provide a link to a quiz and be done with it, but I wanted a little more security. I wanted the ability to store my quiz files outside the normal public_html file space and to have basic password protection so people don’t take a quiz until I know it’s ready. (I don’t release the password until I’m ready for people to take the quiz.) Also, I can easily turn “off” a quiz by simply changing its password. The takeQuiz page’s only real job is to check the user’s password against the indi- cated test’s password and allow access to the quiz if appropriate. 220 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r <? //takeQuiz.php //given a quiz file, prints out that quiz //get the password from the file $masterFile = $takeFile . “.mas”; $fp = fopen($masterFile, “r”); //the password is the third line, so get the first two lines, but ignore them $dummy = fgets($fp); $dummy = fgets($fp); $magicWord = fgets($fp); $magicWord = rtrim($magicWord); fclose($fp); if ($password == $magicWord){ $htmlFile = $takeFile . “.html”; //print out the page if the user got the password right readFile($htmlFile); } else { print <<<HERE <font color = “red” size = +3> Incorrect Password.<br> You must have a password in order to take this quiz </font> HERE; } // end if ?> The password associated with a test is stored in the test file, so once I know which test the user wants to take, I can open that file and extract the password. The password is stored in the file’s third line. The only way to get to it with a sequen- tial access file is to load the first two lines into a dummy variable and then load the password into a variable called $magicWord. If the user indicates a password that matches $magicWord, I use the readFile() function to send the contents of the quiz HTML page to the browser. If not, I send a message indicating the pass- word was incorrect. 221 C h a p t e r 6 W o r k i n g w i t h F i l e s This is a dandy place to set up a little more security. I keep a log file of every access in a production version of this system, so I know if somebody has been try- ing to get at my system 1,000 times from the same machine within a second (a sure sign of some kind of automated attack) or other mischief. I can also check to see that later on when a page has been submitted, it comes from the same com- puter that requested the file in the first place. I can also check the request and submission times to reject quizzes that have been out longer than my time limit. Grading the Quiz One advantage of this kind of system is the potential for instantaneous feedback for the user. As soon as the user clicks the submit button, the gradeQuiz.php pro- gram automatically grades the quiz and stores a results log for the administrator. Opening the Files The gradeQuiz program, like all the programs in this system, relies on files to do all its important work. In this case, the program uses the master file to get the answer key for the quiz and writes to a log file. <? print <<<HERE <html> <head> <title>Grade for $quizName, $student</title> </head> <body> </body> <h1>Grade for $quizName, $student</h1> HERE; //open up the correct master file for reading $fileBase = str_replace(“ “, “_”, $quizName); $masFile = $fileBase . “.mas”; $msfp = fopen($masFile, “r”); $logFile = $fileBase . “.log”; TRICK 222 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r . <<<HERE <center> <h1>$quizName</h1> </center> <form action = “gradeQuiz .php method = “post”> Name <input type = “text” name = “student”> <ol> HERE; $questionNumber. appropriate. 220 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r <? //takeQuiz .php //given a quiz file, prints out that quiz //get the password from the file $masterFile = $takeFile. instantaneous feedback for the user. As soon as the user clicks the submit button, the gradeQuiz .php pro- gram automatically grades the quiz and stores a results log for the administrator. Opening

Ngày đăng: 07/07/2014, 02:20