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

PHP 5/MySQL Programming- P42 ppsx

5 205 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 211,24 KB

Nội dung

Taking a Quiz Users with knowledge of the appropriate password can take any of the quizzes known to the system. If a user chooses to take the Monty Python quiz, the screen shown in Figure 6.3 appears. 183 C h a p t e r 6 W o r k i n g w i t h F i l e s FIGURE 6.1 The user is an administrator preparing to edit a quiz. FIGURE 6.2 The user has chosen to edit the Monty Python quiz. Seeing the Results When the user takes a quiz, the user’s responses are sent to a program that grades the quiz and provides immediate feedback, as shown in Figure 6.4. 184 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 FIGURE 6.3 The user is taking the Monty Python quiz. If you want to become a serious programmer, you should probably rent this movie. It’s part of the culture. FIGURE 6.4 The grading program provides immediate feedback and stores the information in a file so the administrator can see it later. Viewing the Quiz Log The system keeps a log file for each quiz so the administrator can see each per- son’s score. Figure 6.5 shows how people have done on the Monty Python quiz. Although the resulting log looks very simplistic, it is generated in a format that can easily be imported into most gradebook programs and spreadsheets. This is very handy if you use the quiz in a classroom setting. Saving a File to the File System Your PHP programs can access the server’s file system to store and retrieve infor- mation. Your programs can create new files, add data to files, and read information from the files. You start by writing a program that creates a file and adds data to it. Introducing the saveSonnet.php Program The saveSonnet.php program shown in the following code opens a file on the server and writes one of Shakespeare’s sonnets to that file on the server. Normally I show you a screen shot of every program, but that isn’t useful since this particular program doesn’t display anything on the screen. The next couple of pro- grams read this file and display it onscreen. You see what they look like when the time comes. TRICK 185 C h a p t e r 6 W o r k i n g w i t h F i l e s FIGURE 6.5 The log retrieval program presents an activity log for each quiz. <head> <title>SaveSonnet</title> </head> <body> <? $sonnet76 = <<<HERE Sonnet # 76, William Shakespeare Why is my verse so barren of new pride, So far from variation or quick change? Why with the time do I not glance aside To new-found methods, and to compounds strange? Why write I still all one, ever the same, And keep invention in a noted weed, That every word doth almost tell my name, Showing their birth, and where they did proceed? O! know sweet love I always write of you, And you and love are still my argument; So all my best is dressing old words new, Spending again what is already spent: For as the sun is daily new and old, So is my love still telling what is told. HERE; $fp = fopen(“sonnet76.txt”, “w”); fputs($fp, $sonnet76); fclose($fp); ?> </body> </html> Most of the code stores the contents of Shakespeare’s 76th sonnet to a variable called $sonnet76. The remaining three lines save the data in the variable to a text file. 186 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 Opening a File with fopen() The fopen() command opens a file. Note that you can create files on the Web server only—you cannot directly create a file on the client machine, because you do not have access to that machine’s file system. (If you did, any server-side pro- gram would be able to create viruses with extreme ease.) However, as a server-side programmer, you already have the ability to create files on the server. The pro- grams you are writing are files. Your programs can write files as if they are you. The ownership of files created by your PHP programs can be a little more compli- cated, depending on your operating system, server, and PHP configurations. Generally, any file that your program creates is owned by a special user called PHP or by the account you were in when you wrote the program. This makes a big difference in an operating system like UNIX, where file ownership is a major part of the security mechanism. The best way to discover how this works is to write a program that creates a file and then look at that file’s properties. The filename is the first parameter of the fopen() function. This filename can include directory information or it can be a relative reference starting from the current file’s location. Always test your programs, especially if they use a relative reference for a file- name. It’s possible that your current directory is not the default directory. Also, the filename is based on the actual file server system, rather than the file’s URL. You can create a file anywhere on the server to which you have access. Your files can be in the parts of your directory system open to the Web server (usually sub- directories of public_html or htdocs). Sometimes, though, you might not want your files to be directly accessible to users by typing a URL. You can control access to these files as follows: • Place them outside the public HTML space. • Set permissions so they can be read by you (and programs you create) but not by anyone else. Creating a File Handle When you create a file with the fopen() command, the function returns an inte- ger called a file handle (sometimes also called a file pointer ). This special number refers to the file in subsequent commands. You aren’t usually concerned about this handle’s actual value, but need to store it in a variable (I usually use $fp) so your other file-access commands know which file to work with. TRAP TRAP 187 C h a p t e r 6 W o r k i n g w i t h F i l e s . writing a program that creates a file and adds data to it. Introducing the saveSonnet .php Program The saveSonnet .php program shown in the following code opens a file on the server and writes one. are you. The ownership of files created by your PHP programs can be a little more compli- cated, depending on your operating system, server, and PHP configurations. Generally, any file that your. is very handy if you use the quiz in a classroom setting. Saving a File to the File System Your PHP programs can access the server’s file system to store and retrieve infor- mation. Your programs

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