eg-259:lecture18
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
eg-259:lecture18 [2008/12/01 08:20] – eechris | eg-259:lecture18 [2013/03/08 18:04] (current) – [PHP for Web Applications] eechris | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~SLIDESHOW~~ | ||
+ | ====== PHP for Web Applications ====== | ||
+ | **Supplementary Material** | ||
+ | |||
+ | Provided for Reference. This material is no longer taught on this module. | ||
+ | |||
+ | **Lecturer**: | ||
+ | |||
+ | Using PHP for web applications development. | ||
+ | |||
+ | |||
+ | |||
+ | ===== PHP for Web Applications ===== | ||
+ | |||
+ | We conclude our review of the Basics of PHP with a discussion of its use in creating web applications. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | Based on Chapter 12 of Robert W. Sebasta, // | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Contents of this Lecture ===== | ||
+ | |||
+ | //PHP for web applications with examples// | ||
+ | |||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | |||
+ | The [[eg-259: | ||
+ | |||
+ | |||
+ | ===== Learning Outcomes ==== | ||
+ | |||
+ | |||
+ | //At the end of this lecture you should be able to answer these questions//: | ||
+ | |||
+ | - How can the value of a form element be accessed by a PHP script? | ||
+ | - What is a file variable? | ||
+ | - What is a file pointer? | ||
+ | - What does an '' | ||
+ | - Explain the parameters and actions of the '' | ||
+ | |||
+ | |||
+ | ===== Learning Outcomes (continued) ===== | ||
+ | |||
+ | //At the end of this lecture you should be able to answer these questions//: | ||
+ | |||
+ | - What is returned by the '' | ||
+ | - How can a cookie be created in a PHP script? | ||
+ | - How can a script determine whether a particular cookie exists? | ||
+ | - How can a variable be saved in a session? | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Queries and Query Strings ===== | ||
+ | |||
+ | * Parameters sent to a Web Application from a web browser take the form of a //query string// | ||
+ | * The //query string// is either appended to the URI in a GET request as in: | ||
+ | |||
+ | GET / | ||
+ | Host: some.host.com | ||
+ | : | ||
+ | |||
+ | * or is carried in the payload of a POST request as in: | ||
+ | |||
+ | POST /webapp.cgi HTTP/1.1 | ||
+ | Host: some.host.com | ||
+ | : | ||
+ | |||
+ | | ||
+ | |||
+ | |||
+ | ===== Processing GET rquests ===== | ||
+ | |||
+ | * Web server passes two environment variables to helper application | ||
+ | * '' | ||
+ | * The query string will be content of '' | ||
+ | |||
+ | |||
+ | ===== A Real GET request ===== | ||
+ | |||
+ | * //From [[http:// | ||
+ | |||
+ | GET / | ||
+ | Host: localhost | ||
+ | User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.9) Gecko/ | ||
+ | Accept: text/ | ||
+ | Accept-Language: | ||
+ | Accept-Encoding: | ||
+ | Accept-Charset: | ||
+ | Keep-Alive: 300 | ||
+ | Connection: keep-alive | ||
+ | Referer: http:// | ||
+ | |||
+ | ---- | ||
+ | |||
+ | * Web application is ''/ | ||
+ | * Host is '' | ||
+ | * Query string is '' | ||
+ | * Although data comes from a form (// | ||
+ | |||
+ | |||
+ | |||
+ | ===== Processing POST requests ===== | ||
+ | |||
+ | * Data passed to helper application in // | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * Query string is '' | ||
+ | |||
+ | |||
+ | |||
+ | ===== A Real POST request ===== | ||
+ | |||
+ | * //Also from [[http:// | ||
+ | |||
+ | POST / | ||
+ | Host: localhost | ||
+ | User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.9) Gecko/ | ||
+ | Accept: text/ | ||
+ | Accept-Language: | ||
+ | Accept-Encoding: | ||
+ | Accept-Charset: | ||
+ | Keep-Alive: 300 | ||
+ | Connection: keep-alive | ||
+ | Referer: http:// | ||
+ | Content-Type: | ||
+ | Content-Length: | ||
+ | | ||
+ | colour=light& | ||
+ | |||
+ | ---- | ||
+ | |||
+ | * Web application is ''/ | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * Query string is '' | ||
+ | |||
+ | |||
+ | ===== Query String Format ===== | ||
+ | |||
+ | |||
+ | * A query string includes names and values of HTML form elements (widgets) | ||
+ | * Widget values are always coded as strings | ||
+ | * The form of a name/value pair in a query string is: | ||
+ | |||
+ | name=value | ||
+ | |||
+ | * If the form has more than one widget, their values are separated with ampersands: | ||
+ | |||
+ | milk=2& | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== URL Specification ===== | ||
+ | |||
+ | * The specification for URLs ([[http:// | ||
+ | |||
+ | > " | ||
+ | |||
+ | * Any source of general text that appears in an HTML page that may be represented in a URL has to be encoded to ensure that illegal characters do not appear. | ||
+ | * This includes widget names, and widget values. | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== URL Encoding ===== | ||
+ | |||
+ | * Performed automatically by Browser on form submission | ||
+ | * Each special character is encoded as a percent sign and a two-character hexadecimal number (the ASCII code for the character) | ||
+ | * Some browsers code spaces as plus signs, rather than as '' | ||
+ | * You should URL encode any text that will be sent to the browser in a form widget, or in the '' | ||
+ | |||
+ | ---- | ||
+ | |||
+ | For more more information on URL encoding see ((Brian Wilson, URL Encoding (or: 'What are the " | ||
+ | |||
+ | |||
+ | ===== URL Encoding – Examples ===== | ||
+ | |||
+ | * URL encode general text (e.g. from file input or results of a previous form submission) | ||
+ | <code php> | ||
+ | <?php | ||
+ | echo '<a href=" | ||
+ | ?> | ||
+ | </ | ||
+ | * URL encode user input, then convert results into valid HTML (e.g. & -> &) | ||
+ | <code php> | ||
+ | <?php | ||
+ | $query_string = ' | ||
+ | echo '<a href=" | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== PHP and Query Data ===== | ||
+ | |||
+ | * PHP takes care of extracting the query parameters for you | ||
+ | * It maps the HTTP request fields into PHP variables | ||
+ | * It extracts the query string from the HTTP request, extracts the name/value pairs and places them into an array. | ||
+ | * Array '' | ||
+ | * value subscripts are the widget names | ||
+ | * This is transparent and it does not matter whether GET or POST method is used to transmit the form data | ||
+ | | ||
+ | |||
+ | |||
+ | ===== Query parameters ===== | ||
+ | |||
+ | |||
+ | * If the query string has '' | ||
+ | * '' | ||
+ | * Query matching will also turn check box group '' | ||
+ | |||
+ | |||
+ | ===== Form Handling ===== | ||
+ | |||
+ | |||
+ | * Simple in PHP | ||
+ | * Forms could be handled by the same document that creates the form, but that may be confusing | ||
+ | * Better to separate " | ||
+ | |||
+ | |||
+ | ===== Example – Popcorn Sales ===== | ||
+ | |||
+ | * //The Form Page (just HTML)// : [[/ | ||
+ | |||
+ | < | ||
+ | <form action = " | ||
+ | method = " | ||
+ | <h2> Welcome to Millennium Gymnastics Booster Club Popcorn | ||
+ | Sales </h2> | ||
+ | < | ||
+ | <!-- Text widgets for the customer' | ||
+ | <tr> | ||
+ | <td> Buyer' | ||
+ | <td> | ||
+ | <input type = " | ||
+ | size = " | ||
+ | </td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td> Street Address: </td> | ||
+ | <td> | ||
+ | <input type = " | ||
+ | size = " | ||
+ | </td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td> City, State, Zip: </td> | ||
+ | <td> | ||
+ | <input type = " | ||
+ | size = " | ||
+ | </td> | ||
+ | </tr> | ||
+ | </ | ||
+ | <p /> | ||
+ | <table border = " | ||
+ | <!-- First, the column headings --> | ||
+ | <tr> | ||
+ | <th> Product </th> | ||
+ | <th> Price </th> | ||
+ | <th> Quantity </th> | ||
+ | </tr> | ||
+ | <!-- Now, the table data entries --> | ||
+ | <tr> | ||
+ | <td> Unpopped Popcorn (1 lb.) </td> | ||
+ | <td> $3.00 </td> | ||
+ | <td align = " | ||
+ | <input type = " | ||
+ | size = " | ||
+ | </td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td> Caramel Popcorn (2 lb. canister) </td> | ||
+ | <td> $3.50 </td> | ||
+ | <td align = " | ||
+ | <input type = " | ||
+ | size = " | ||
+ | </td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td> Caramel Nut Popcorn (2 lb. canister) </td> | ||
+ | <td> $4.50 </td> | ||
+ | <td align = " | ||
+ | <input type = " | ||
+ | size = " | ||
+ | </td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td> Toffey Nut Popcorn (2 lb. canister) </td> | ||
+ | <td> $5.00 </td> | ||
+ | <td align = " | ||
+ | <input type = " | ||
+ | size = " | ||
+ | </td> | ||
+ | </tr> | ||
+ | </ | ||
+ | <p /> | ||
+ | <!-- The radio buttons for the payment method --> | ||
+ | <h3> Payment Method </h3> | ||
+ | <p> | ||
+ | <input type = " | ||
+ | checked = " | ||
+ | Visa | ||
+ | <br /> | ||
+ | <input type = " | ||
+ | Master Card | ||
+ | <br /> | ||
+ | <input type = " | ||
+ | value = " | ||
+ | Discover | ||
+ | <br /> | ||
+ | <input type = " | ||
+ | Check | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- The submit and reset buttons --> | ||
+ | <input type = " | ||
+ | <input type = " | ||
+ | </p> | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <code html> | ||
+ | < | ||
+ | <!-- popcorn3.html - This describes the popcorn sales form --> | ||
+ | <html lang=" | ||
+ | < | ||
+ | <meta charset=" | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | <form action = " | ||
+ | method = " | ||
+ | <h2> Welcome to Millennium Gymnastics Booster Club Popcorn | ||
+ | Sales </h2> | ||
+ | < | ||
+ | <!-- Text widgets for the customer' | ||
+ | <tr> | ||
+ | <td> Buyer' | ||
+ | <td> | ||
+ | <input type = " | ||
+ | size = " | ||
+ | </td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td> Street Address: </td> | ||
+ | <td> | ||
+ | <input type = " | ||
+ | size = " | ||
+ | </td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td> City, State, Zip: </td> | ||
+ | <td> | ||
+ | <input type = " | ||
+ | size = " | ||
+ | </td> | ||
+ | </tr> | ||
+ | </ | ||
+ | <p /> | ||
+ | <table border = " | ||
+ | <!-- First, the column headings --> | ||
+ | <tr> | ||
+ | <th> Product </th> | ||
+ | <th> Price </th> | ||
+ | <th> Quantity </th> | ||
+ | </tr> | ||
+ | <!-- Now, the table data entries --> | ||
+ | <tr> | ||
+ | <td> Unpopped Popcorn (1 lb.) </td> | ||
+ | <td> $3.00 </td> | ||
+ | <td align = " | ||
+ | <input type = " | ||
+ | size = " | ||
+ | </td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td> Caramel Popcorn (2 lb. canister) </td> | ||
+ | <td> $3.50 </td> | ||
+ | <td align = " | ||
+ | <input type = " | ||
+ | size = " | ||
+ | </td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td> Caramel Nut Popcorn (2 lb. canister) </td> | ||
+ | <td> $4.50 </td> | ||
+ | <td align = " | ||
+ | <input type = " | ||
+ | size = " | ||
+ | </td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td> Toffey Nut Popcorn (2 lb. canister) </td> | ||
+ | <td> $5.00 </td> | ||
+ | <td align = " | ||
+ | <input type = " | ||
+ | size = " | ||
+ | </td> | ||
+ | </tr> | ||
+ | </ | ||
+ | <p /> | ||
+ | <!-- The radio buttons for the payment method --> | ||
+ | <h3> Payment Method </h3> | ||
+ | <p> | ||
+ | <input type = " | ||
+ | checked = " | ||
+ | Visa | ||
+ | <br /> | ||
+ | <input type = " | ||
+ | Master Card | ||
+ | <br /> | ||
+ | <input type = " | ||
+ | value = " | ||
+ | Discover | ||
+ | <br /> | ||
+ | <input type = " | ||
+ | Check | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- The submit and reset buttons --> | ||
+ | <input type = " | ||
+ | <input type = " | ||
+ | </p> | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Example – Popcorn Sales Receipt ===== | ||
+ | |||
+ | |||
+ | * //The results page -- HTML with embedded PHP//: popcorn3.php | ||
+ | |||
+ | ---- | ||
+ | <code php> | ||
+ | < | ||
+ | <!-- popcorn3.php - Processes the form described in | ||
+ | popcorn3.html | ||
+ | --> | ||
+ | <html lang=" | ||
+ | < | ||
+ | <meta charset=" | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | <?php | ||
+ | // Get form data values | ||
+ | $unpop=$_POST[" | ||
+ | $caramel=$_POST[" | ||
+ | $caramelnut=$_POST[" | ||
+ | $toffeynut=$_POST[" | ||
+ | $name=$_POST[" | ||
+ | $street=$_POST[" | ||
+ | $city=$_POST[" | ||
+ | $payment=$_POST[" | ||
+ | // If any of the quantities are blank, set them to zero | ||
+ | if($unpop=="" | ||
+ | $unpop=0; | ||
+ | if($caramel=="" | ||
+ | $caramel=0; | ||
+ | if($caramelnut=="" | ||
+ | $caramelnut=0; | ||
+ | if($toffeynut=="" | ||
+ | $toffeynut=0; | ||
+ | // Compute the item costs and total cost | ||
+ | $unpop_cost=3.0*$unpop; | ||
+ | $caramel_cost=3.5*$caramel; | ||
+ | $caramelnut_cost=4.5*$caramelnut; | ||
+ | $toffeynut_cost=5.0*$toffeynut; | ||
+ | $total_price=$unpop_cost+$caramel_cost+$caramelnut_cost+$toffeynut_cost; | ||
+ | $total_items=$unpop+$caramel+$caramelnut+$toffeynut; | ||
+ | // Return the results to the browser in a table | ||
+ | ?> | ||
+ | <h4> Customer: </h4> | ||
+ | <?php | ||
+ | print(" | ||
+ | ?> | ||
+ | <p /> | ||
+ | <p /> | ||
+ | <table border = " | ||
+ | < | ||
+ | Order Information | ||
+ | </ | ||
+ | <tr> | ||
+ | <th> Product </th> | ||
+ | <th> Unit Price </th> | ||
+ | <th> Quantity Ordered </th> | ||
+ | <th> Item Cost </th> | ||
+ | </tr> | ||
+ | <tr align = " | ||
+ | <td> Unpopped Popcorn </td> | ||
+ | <td> $3.00 </td> | ||
+ | < | ||
+ | < | ||
+ | </tr> | ||
+ | <tr align = " | ||
+ | <td> Caramel Popcorn </td> | ||
+ | <td> $3.50 </td> | ||
+ | < | ||
+ | < | ||
+ | </tr> | ||
+ | <tr align = " | ||
+ | <td> Caramel Nut Popcorn </td> | ||
+ | <td> $4.50 </td> | ||
+ | < | ||
+ | < | ||
+ | </tr> | ||
+ | <tr align = " | ||
+ | <td> Toffey Nut Popcorn </td> | ||
+ | <td> $5.00 </td> | ||
+ | < | ||
+ | < | ||
+ | </tr> | ||
+ | </ | ||
+ | <p /> | ||
+ | <p /> | ||
+ | <?php | ||
+ | print(" | ||
+ | printf(" | ||
+ | print(" | ||
+ | ?> | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | * Output | ||
+ | |||
+ | < | ||
+ | <h4> Customer: </h4> | ||
+ | < | ||
+ | <table border = " | ||
+ | < | ||
+ | <tr> | ||
+ | <th> Product </th> | ||
+ | <th> Unit Price </th> | ||
+ | <th> Quantity Ordered </th> | ||
+ | <th> Item Cost </th> | ||
+ | </tr> | ||
+ | <tr align = " | ||
+ | <td> Unpopped Popcorn </td> | ||
+ | <td> $3.00 </td> | ||
+ | <td> 0 </td> | ||
+ | <td> $ 0.00 </td> | ||
+ | </tr> | ||
+ | <tr align = " | ||
+ | <td> Caramel Popcorn </td> | ||
+ | <td> $3.50 </td> | ||
+ | <td> 0 </td> | ||
+ | <td> $ 0.00 </td> | ||
+ | </tr> | ||
+ | <tr align = " | ||
+ | <td> Caramel Nut Popcorn </td> | ||
+ | <td> $4.50 </td> | ||
+ | <td> 0 </td> | ||
+ | <td> $ 0.00 </td> | ||
+ | </tr> | ||
+ | <tr align = " | ||
+ | <td> Toffey Nut Popcorn </td> | ||
+ | <td> $5.00 </td> | ||
+ | <td> 0 </td> | ||
+ | <td> $ 0.00 </td> | ||
+ | </tr> | ||
+ | </ | ||
+ | <p /> <p /> | ||
+ | |||
+ | You ordered 0 popcorn items <br />Your total bill is: $ 0.00 <br />Your chosen method of payment is: <br /> | ||
+ | </ | ||
+ | |||
+ | ===== Files ===== | ||
+ | |||
+ | |||
+ | * PHP can: | ||
+ | * Deal with any files on the server | ||
+ | * Deal with any files on the Internet, using either HTTP or FTP | ||
+ | * Instead of filehandles, | ||
+ | * A file has a //file pointer// (where to read or write) | ||
+ | |||
+ | |||
+ | ===== Opening a File ===== | ||
+ | |||
+ | |||
+ | * Command to open a file and assign to a file variable: | ||
+ | <code php> | ||
+ | $fptr = fopen(filename, | ||
+ | </ | ||
+ | |||
+ | ===== File use indicators ===== | ||
+ | ^ Indicator ^ Purpose ^ | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | |||
+ | |||
+ | ===== File opening and closing ===== | ||
+ | |||
+ | * Because //fopen// could fail, use it with die: | ||
+ | <code php> | ||
+ | $file_var = fopen(" | ||
+ | die (" | ||
+ | </ | ||
+ | * Use '' | ||
+ | * Use '' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== File use cases ===== | ||
+ | |||
+ | - [[# | ||
+ | - [[# | ||
+ | - [[# | ||
+ | - [[# | ||
+ | |||
+ | |||
+ | |||
+ | ===== Read all or part of the file into a string variable ===== | ||
+ | |||
+ | <code php> | ||
+ | $str = fread(file_var, | ||
+ | </ | ||
+ | * To read the whole file, use '' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Read the lines of the file into an array ===== | ||
+ | |||
+ | <code php> | ||
+ | $file_lines = file(file_name) | ||
+ | </ | ||
+ | * Need not open or close the file | ||
+ | |||
+ | |||
+ | ===== Read one line from the file ===== | ||
+ | |||
+ | <code php> | ||
+ | $line = fgets(file_var, | ||
+ | </ | ||
+ | * Reads characters until //eoln//, //eof//, or '' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Read one character at a time ===== | ||
+ | |||
+ | <code php> | ||
+ | $ch = fgetc(file_var) | ||
+ | </ | ||
+ | * Control reading lines or characters with //eof// detection using '' | ||
+ | <code php> | ||
+ | while( ! feof($file_var) ) { | ||
+ | $ch = fgetc($file_var); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Writing to files ===== | ||
+ | |||
+ | |||
+ | * To write to a file use: | ||
+ | <code php> | ||
+ | $bytes_written = fwrite(file_var, | ||
+ | </ | ||
+ | * '' | ||
+ | * Files can be locked (to avoid interference from concurrent accesses) with '' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Storing Information about Client State ===== | ||
+ | |||
+ | * A session is the time span during which a browser interacts with a particular server | ||
+ | * The HTTP protocol is stateless | ||
+ | * But, there are several reasons why it is useful for the server to relate a request to a session | ||
+ | * Shopping carts for many different simultaneous customers | ||
+ | * Customer profiling for advertising | ||
+ | * Customized interfaces for specific clients | ||
+ | * Approaches to storing client information: | ||
+ | * Store it on the server -- often too much to store! | ||
+ | * Store it on the client machine -- this works | ||
+ | |||
+ | ===== Session Tracking with Cookies ===== | ||
+ | |||
+ | |||
+ | * A cookie is a small object of information consisting of a name and a textual value | ||
+ | * Cookies are created by some software system on the server | ||
+ | * Every HTTP communication between the browser and the server includes information in its header about the message | ||
+ | * At the time a cookie is created, it is given a lifetime | ||
+ | * Every time the browser sends a request to the server that created the cookie, while the cookie is still alive, the cookie is included | ||
+ | * A browser can be set to reject all cookies | ||
+ | |||
+ | |||
+ | |||
+ | ===== Baking Cookies ===== | ||
+ | |||
+ | * Create a cookie with setcookie: | ||
+ | <code php> | ||
+ | setcookie(cookie_name, | ||
+ | </ | ||
+ | * e.g.: | ||
+ | <code php> | ||
+ | setcookie(" | ||
+ | </ | ||
+ | * Cookies must be created before any other HTML is created by the script | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | < | ||
+ | // | ||
+ | </ | ||
+ | |||
+ | ===== Consuming Cookies ===== | ||
+ | |||
+ | * Cookies are obtained in a script the same way form values are obtained, using the '' | ||
+ | |||
+ | ===== Session Tracking ===== | ||
+ | |||
+ | |||
+ | * For session tracking, PHP creates and maintains a session tracking id | ||
+ | * Create the id with a call to '' | ||
+ | * Subsequent calls to '' | ||
+ | |||
+ | ===== Session Tracking ===== | ||
+ | |||
+ | |||
+ | * To create a session variable, use the global array '' | ||
+ | |||
+ | ===== Session Tracking: Example ===== | ||
+ | |||
+ | |||
+ | * To count number of pages visited in a web site, put the following code in all documents: | ||
+ | <code php> | ||
+ | <?php session_start(); | ||
+ | ?> | ||
+ | : | ||
+ | <?php | ||
+ | if (!IsSet($_SESSION[' | ||
+ | $_SESSION[' | ||
+ | | ||
+ | | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Summary of this Lecture ===== | ||
+ | |||
+ | //PHP for web applications with examples// | ||
+ | |||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | |||
+ | |||
+ | The [[eg-259: | ||
+ | |||
+ | |||
+ | |||
+ | ===== Learning Outcomes ==== | ||
+ | |||
+ | |||
+ | //At the end of this lecture you should be able to answer these questions//: | ||
+ | |||
+ | - How can the value of a form element be accessed by a PHP script? | ||
+ | - What is a file variable? | ||
+ | - What is a file pointer? | ||
+ | - What does an '' | ||
+ | - Explain the parameters and actions of the '' | ||
+ | |||
+ | |||
+ | ===== Learning Outcomes (continued) ===== | ||
+ | |||
+ | //At the end of this lecture you should be able to answer these questions//: | ||
+ | |||
+ | - What is returned by the '' | ||
+ | - How can a cookie be created in a PHP script? | ||
+ | - How can a script determine whether a particular cookie exists? | ||
+ | - How can a variable be saved in a session? | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Homework Exercise ===== | ||
+ | |||
+ | Write, test, and debug (if necessary) a PHP script for the following function. Write functions and the code to test them. | ||
+ | |||
+ | - // | ||
+ | |||
+ | There are more exercises [[eg-259: | ||
+ | |||
+ | |||
+ | |||
+ | ===== What's Next? ===== | ||
+ | |||
+ | **Database Access through the Web** | ||
+ | |||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | * [[eg-259: | ||
+ | |||
+ | |||
+ | |||
+ | [[eg-259: |