| WebSys Pizza Server (Cookies) |
|   WebSys Home   |   Try it |
This sample PHP program demonstrates the use of HTTP Cookies
to provide a session. This system involves a single
program, although this program can handle a number of different
kinds of requests. The examples we have discussed in the past have
involved multiple CGI programs, this one involves only one. The single
program (pizza.php) can act in different ways
depending on the query received, specifically it looks at the form
field named "formname" and does the following:
Value of formname | Resulting Action |
|---|---|
| none | If a valid cookie is sent, send back an order form. If no cookie (or not valid), sends back a login form. |
login |
Validates the name and password, and if valid sends back an order form. Also sets a cookie named WEBSYSPIZZA with the user name as the value. |
order |
Validates the order and if valid sends back a receipt |
The system includes a login form that allows a user to log on to the system using a name and password. The program reads names and passwords from a file name "pw". The username "joe" and the password "pizza" are already set up in the file "pw".
Once the user logs in, the program sends back a pizza order form and a cookie that identifies the user. When the order form is submitted, the order is processed only if the user's name is found in the cookie that comes as part of the request.
Keep in mind that anyone who knows a valid user name could construct an HTTP request including a cookie that looks like a pizza order. In general cookies are used with session keys to avoid this situation...
Much of the HTML that is generated by the PHP program is read from files. This makes it possible to change the look of the site without changing the PHP program itself.
| pizza.php | The PHP program. One program handles the entire system. Click on this link and you will get the login page. See below to actually see the source code. |
| logo | The WebSys Pizza logo that shows up on the top of all pages. |
| pizzaguy.gif | A graphic image included in the logo. |
| loginform.tmpl | The HTML form a user uses to log in to the pizza server. |
| orderform.tmpl | Most of the HTML form for ordering a pizza. |
| pizzaphp.html | Source code for the PHP program |
| pizza.zip | Zip file containing everything. |