An online order system is not very useful if it cannot


Advanced Server-Side Scripting

As you gain experience with server-side scripting, you discover increasingly sophisticated ways to improve the user experience. In this Application, you explore the techniques of file storage and session variables.

To prepare for this Application:

Saving to a File

An online order system is not very useful if it cannot remember the orders it accepts. Operating systems allow programs to read and write files so that they may store information. Websites frequently rely on more sophisticated database management systems, but that is beyond the scope of this course. The fundamental operations you use on a file are:

- Open: Find or create the file and make it available to the program

- Read: Get content from the file

- Write: Add or modify content in a file

- Close: Disconnect the file from the program

In your order processing page, add the code necessary to save to a file the date, name, age, and item for every order. Which use indicator allows the program to add new data to the end of the file? Does each order appear on a separate line in your file? Write the escape code "\n" (backslash followed immediately by the letter "n") at the end of the file output. This code tells PHP to end the line there. The next record will start on a new line.

How can you tell where one value ends and the next value begins in the file? You could try separating the fields with a comma or some other specific character. What problems do you see with this method? How can you address them? Include your completed PHP source code and a screenshot of the file contents.

Using Session Variables

Websites often rely on client cookies to remember information (called "state") about a user. The Web browser can store cookies for a specific or an indefinite period. Developers also use server-side session keys to retain state for a limited time.

You now use session variables to remember a user's name and age. The idea is to save a customer from having to provide the same information several times in a session. Add PHP code to your "catalog" order-processing page to:

- Call the session_start function.

- Detect whether or not the name session variable has a value (use the array_key_exists function); if not, use the name and age post values to set the name and age session variables.

- Use the session variables, not the post variables, in the confirmation message and when writing to the file.
The effect of these changes is to remember the user's name and age after the user has a successful submission. Verify that this page still works properly. Next, modify your "catalog" page to:

- Call the session_start function.

- Detect whether or not the name session variable has a value; if so, display name and age as text; if not, display the name and age input fields.

These changes take advantage of the saved information. Describe the additional changes you would have to make to "forget" a user's name and age.

Solution Preview :

Prepared by a verified Expert
PHP Web Programming: An online order system is not very useful if it cannot
Reference No:- TGS02742459

Now Priced at $60 (50% Discount)

Recommended (97%)

Rated (4.9/5)