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

PHP 5/MySQL Programming- P48 docx

5 161 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 86,63 KB

Nội dung

Invalid Password! </font> HERE; } else { //check to see if user has chosen a form to edit if ($editFile == “new”){ //if it’s a new file, put in some dummy values $quizName = “sample test”; $quizEmail = “root@localhost”; $quizData = “q:a:b:c:d:correct”; $quizPwd = “php”; } else { //open up the file and get the data from it $fp = fopen($editFile, “r”); $quizName = fgets($fp); $quizEmail = fgets($fp); $quizPwd = fgets($fp); while (!feof($fp)){ $quizData .= fgets($fp); } // end while fclose($fp); } // end ‘new form’ if I decided to code the value absolute (from the name of this book series) as an administrative password. Each test has its own password and the administrative functions (like editing a quiz) have their own passwords. If the password field has any other value besides my chosen password, the program indicates a problem and refuses to move forward. An administrative password keeps casual snoops out of your system, but it’s nowhere near bullet-proof security. This system is not appropriate for situations where you must absolutely secure the tests. Once you know the user is authorized to edit tests, determine if it’s a new or existing quiz. If the quiz is new, I simply add sample data to the variables, which are used for the upcoming form. If the user wants to see an existing test, I open the file for read access and grab the first three lines, which correspond to the $quizName, $quizEmail, and $quizPwd fields. A foreach loop loads the rest of the file into the $quizData variable. TRAP 213 C h a p t e r 6 W o r k i n g w i t h F i l e s You might wonder why the quiz needs a password field if it took a password to get to this form. The quiz system has multiple levels of security. Anybody can get to the quizBuilder.php page. However, to move to one of the other pages, the user must have the right kind of password. Only an administrator should go to the editPage and showLog programs, so these programs require special adminis- trative password access. Each quiz also has an associated password. The quiz master file stores the password so you can associate a different password for each quiz. In this way, the users authorized to take one test won’t take other tests (and confuse your log files). Printing the Form Once the variables are loaded with appropriate values, it’s a simple matter to print an HTML form and let the user edit the quiz. The form is almost all pure HTML with the quiz variables interpolated into the appropriate places: print <<<HERE <form action = “writeQuiz.php” method = “post”> <table border = 1> <tr> <th>Quiz Name</th> <td> <input type = “text” name = “quizName” value = “$quizName”> </td> </tr> <tr> <th>Instructor email</th> <td> <input type = “text” name = “quizEmail” value = “$quizEmail”> </td> </tr> TRICK 214 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 <tr> <th>Password</th> <td> <input type = “text” name = “quizPwd” value = “$quizPwd”> <tr> <td rowspan = 1 colspan = 2> <textarea name = “quizData” rows = 20 cols = 60> $quizData</textarea> </td> </tr> <tr> <td colspan = 2><center> <input type = “submit” value = “make the quiz”> </center></td> </tr> </table> </form> HERE; } // end if ?> </body> </html> Writing the Test The administrator has finished editing a quiz file. Now what? That quiz file must be stored to the file system and an HTML page generated for the quiz. The writeQuiz.php program performs these duties. 215 C h a p t e r 6 W o r k i n g w i t h F i l e s Setting Up the Main Logic Creating two files is your first job. The quiz name can be the filename’s foundation, but many file systems choke at spaces within filenames. I use the str_replace() function to replace all spaces in $quizName to underscore characters (_). Then I cre- ate a filename ending in .mas for the master file and another filename ending in .html for the actual quiz. To create the HTML file, I open it for write output. Then I use the buildHTML() function (described shortly) to build the HTML code, write that code to the HTML file, and close the file. The master file is built pretty much the same way, except it calls the buildMas() function to create the appropriate text for the file. <html> <head> <title>Write Quiz</title> </head> <body> <? //given a quiz file from editQuiz, //generates a master file and an HTML file for the quiz //open the output file $fileBase = str_replace(“ “, “_”, $quizName); $htmlFile = $fileBase . “.html”; $masFile = $fileBase . “.mas”; $htfp = fopen($htmlFile, “w”); $htData = buildHTML(); fputs($htfp, $htData); fclose($htfp); $msfp = fopen($masFile, “w”); $msData = buildMas(); fputs($msfp, $msData); fclose($msfp); //preview the actual master file print <<<HERE <pre> $msData </pre> HERE; 216 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 To make sure things are going well, I add a check to the end of the page that prints out the master file’s actual contents. This program’s output lets the administrator see that the test is working correctly. The administrator can take the test and submit it to the grading program from this page. If there is a prob- lem, it’s convenient to have the actual contents of the .mas file visible on the page. Of course, the final HTML page does not contain this data, because it holds the answers. Building the Master File The master file routine is very straightforward: function buildMas(){ //builds the master file global $quizName, $quizEmail, $quizPwd, $quizData; $msData = $quizName . “\n”; $msData .= $quizEmail . “\n”; $msData .= $quizPwd . “\n”; $msData .= $quizData; return $msData; } // end buildMas The critical part is remembering the file structure rules, so any program that reads this file doesn’t get confused. The elements come in this order: • Quiz name • A newline character • The $quizEmail variable • The $quizPwd variable • All $quizData (usually several lines) Note that the function doesn’t actually store the data to the file, but returns it to the main program. This allows me to write the data to both the file and to the page. Building the HTML File The function that creates the HTML is a little more involved, but is manageable. The basic strategy is this: Build an HTML form containing all the questions. For each line of the master file, build a radio group. Place the question and all the possible answers in a set of nested <ol> elements. At the end of the page, there should be one submit button. When the user clicks the submit button, the system calls the gradeQuiz.php page, which evaluates the user’s responses. 217 C h a p t e r 6 W o r k i n g w i t h F i l e s . values $quizName = “sample test”; $quizEmail = “root@localhost”; $quizData = “q:a:b:c:d:correct”; $quizPwd = php ; } else { //open up the file and get the data from it $fp = fopen($editFile, “r”); $quizName. this form. The quiz system has multiple levels of security. Anybody can get to the quizBuilder .php page. However, to move to one of the other pages, the user must have the right kind of password variables interpolated into the appropriate places: print <<<HERE <form action = “writeQuiz .php method = “post”> <table border = 1> <tr> <th>Quiz Name</th> <td> <input

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