module8-cookiesSessions.pdf - CS 85 PHP PROGRAMMING Module...

This preview shows page 1 - 4 out of 13 pages.

Module 10: Cookies & Sessions CS 85: PHP PROGRAMMINGSanta Monica College Computer Science & Information Systems Dept.
When the HTTP protocol was initially developed, there was no consideration taken for tracking an individual user visits to a webpage. Meaning every user that visited the website was identical as far the web server was concern. This is known as a stateless design. Over time there became a need to provide different users a different experience or to track different users individually on website. For website that require a login or a portal to access specific information depending on the user. It is these site that require statefull design. This module will cover the different techniques in PHP to track individual users stateinformation when accessing a website. This module will cover how to use Cookies and Sessions to store user information. Cookies Cookies are small pieces of data stored as text on the client's computer. Normally cookies are used only to store small amounts of data, including user preferences, time and more. Even though cookies are not harmful some people do not permit cookies due to concerns about their privacy. There are two types of cookies: Temporary and Persistent. Temporary Cookies remain on the system only for the life of the browser. Once the browser window closes the cookies are removed. Persistent cookies remain on the system in a text files for a specified period of time. Once cookies are saved on a computer, each time the user visit the website the saved cookies are sent from the client to the server. The server will then interpret the cookies information and provide a customized webpage or information. For example, a web server could receive user information in the cookies, then greet them with a welcome message or access to their user information. PHP allows easy setting and retrieving of cookies. Setting a cookie Setting a cookie is extremely easy with setcookie(). Syntax setcookie(name, value, expire, path, domain, secure, httponly); where name is the cookie name, value is the data to be contained in the cookie, expire the time after which the cookie should expire in seconds, it could be one minutes or 1 week. If expiresis set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes). The argument path is the path on the server which can use the cookie, domain can be used to set permissions for subdomains and secure if set true only transmits the cookie if a secure connection is present. Since all cookies are sent by the server along with HTTP headers you need to set any cookie at the start of a page beforeany other code. You will normally only need to use the name, value
and expire arguments. If expire not set the cookie will expire when the client closes the browser.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture