Tt
Click this widget to change the font size.
CC
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Links | Search | Bio |

Guide to PHP and MySQL (Computer Science 4 All Version)


Chapter 13: Cookies and Sessions

Cookies

You can use cookies to store data in a text file on the user's machine. The setcookie() function can be used to create a cookie. Cookies are stored in the $_COOKIE[] superglobal array which can be used to reteive these cookies at a later time. For example, you can store a user's username and date/time of last login.

<?php
$int = 864000 * 30; // 86400 = 1 day worth of seconds
setcookie(("username", "$myUserid", time() + $int);
?>
Figure 13-1: Using setcookie() to Store Data

You can then use a foreach loop to access that cookie data.

<?php
foreach ($_COOKIE as $key => $val) {
  echo("<br/>$key = $val<br/>\n");
}
?>
Figure 13-2: Retreiving Cookie Data

Figure 13-3: Retreiving Cookie Data Output

Session Data

You can use the PHP session_start() function to create a session and session_destroy() to delete a sessin and its variables. The session id is stored in PHPSESSID cookie. Session variables allow you to store user information that can then be used across multiple pages (e.g. username, first name, last name, isAdmin, etc.). Session variables last until the user closes the browser. Session variables hold information about one single user, and are available to all pages in one application. This data is stored in the $_SESSION[] superglobal array.

<?php
session_start();
$_SESSION['firstName'] = "Jim";
$_SESSION['lastName'] = "Gerland";
?>
Figure 13-4: Using session_start() to Create Session Data

You can then use a foreach loop to access that session data.

<?php
foreach ($_SESSION as $key => $val) {
  echo("<br/>$key = $val<br/>\n");
}
?>
Figure 13-5: Retreiving Session Data
firstName = Jim
lastName = Gerland


Figure 13-6: Retreiving Session Data Output

Task - Create a PHP Web Site

For this task you will create a well-designed electronic system web site demonstrating your understanding of good design principles and appropriate PHP and MySQL usage. You may choose the type of system (retail, business-to-business, non-profit fund raising, auction, classified ads, etc.).

  1. This site should contain (at a minimum):
    1. a home page (about this site),
    2. a login page (reguires a back-end registraton system) with:
      1. a register page
      2. a forgot password page
    3. a database-driven table page (inventory or classified offerings for example),
    4. back-end catalog and user maintenance systems accessible only by a user designated as an 'admin',
    5. a page displaying an order form along with an appropriate "success/thanks" page,
    6. a back-end set of tables to store shopping cart and orders information,
    7. a "Welcome Back" page that displays a user's orders and shopping cart (if one exists) after the user logs in,
    8. a final "Contact Us" form page.
  2. Use well-designed navigation so that the user can get to any page from any other page in this site. You may want to create a separate nav.php file and use the php_include_once to use the same navigation throughout the site.
  3. Modify your JavaScript in your index.php file that uses an array of the Assignments for this course to display an ordered list of these assignments to convert the entry for this assignment to a web link that allows me download and save your web site pages and your, nav.php, vars.php, functions.php, functions.js , and any other files so I can download, save, and grade your work.

You will modify the PHP function for this item in your index.php page to add a link to your web site files that allows me to view your work.

You *MUST* use the W3C Unicorn Validator to validate your HTML5/CSS3 code.

Help contribute to my OER Resources. Donate with PayPal button via my PayPal account.
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2024 Jim Gerland