Professional Documents
Culture Documents
Chapter 6 Cookies and Sessions
Chapter 6 Cookies and Sessions
Chapter 6 Cookies and Sessions
CHAPTER 6:
Cookies and Sessions
Topics covered:-
Cookies
Sessions
1
AMIT 2043 Web Systems and Technologies
Chapter 6 Cookies and Sessions Slide 2
Learning Outcomes
At the end of this chapter, you should be able to
• Use cookies to save state information
• Use sessions to save state information
• Write PHP login scripts to improve security
• Implement a simple online shopping store
2
AMIT 2043 Web Systems and Technologies
Chapter 6 Cookies and Sessions Slide 3
Introduction
• Information about individual visits to a Web site is called state
information
• HTTP is a stateless technology – each individual HTML page is
an unrelated entity.
• PHP tools for storing persistent information about Web site
visits:
• Cookies
• Sessions
3
AMIT 2043 Web Systems and Technologies
Chapter 6 Cookies and Sessions Slide 4
Introduction
• Some examples on the usage of cookies and sessions:
• Customize individual Web pages based on user preferences
• Temporarily store information for a user as a browser
navigates within a multipart form
• Provide shopping carts that store order information
• Store user IDs and passwords
• Use counters to keep track of how many times
a user has visited a site
4
AMIT 2043 Web Systems and Technologies
Chapter 6 Cookies and Sessions Slide 5
2. Cookies
• Cookies are small pieces of information about a user that are
stored by a Web server in text files on the user’s computer
• Temporary cookies remain available only for the current browser
session
• Persistent cookies remain available beyond the current browser
session and are stored in a text file on a client computer
• Users can choose whether to accept cookies that a script attempts
to write to their system
• Cookies are limited to about 4kb of total data, and each browser
remember a limited no. of cookies (50 for most of the current
Web browsers) from any one site.
5
AMIT 2043 Web Systems and Technologies
Chapter 6 Cookies and Sessions Slide 6
Examples:
setcookie(‘name’, ‘John’, time()+3600, ‘/’, ‘.yahoo.com’, 0,0);
setcookie(‘user_id’, $id);
6
AMIT 2043 Web Systems and Technologies
Chapter 6 Cookies and Sessions Slide 7
<?php // cookie1.php
setcookie('name', 'John Smith');
echo "Go to <a href='cookie2.php'>cookie2.php</a> to read it.";
?>
cookies
8
AMIT 2043 Web Systems and Technologies
Chapter 6 Cookies and Sessions Slide 9
If account is
verified to be valid,
the browser is
redirected to
another URL.
10
AMIT 2043 Web Systems and Technologies CookiesSessions\login_cookies.php
Chapter 6 Cookies and Sessions Slide 11
3. Sessions
• A session refers to a period of activity when a PHP script stores
state information on a Web server
• Sessions allow one to maintain state information even when
clients disable cookies in their Web browsers
• Advantages of sessions :
As data is retained on the server, they are more secure
They allow for more data to be stored.
• Advantages of cookies
They are easier to program.
They require less of the server.
<?php
session_start();
$_SESSION = array();
session_destroy();
?>
onlineStore
15
AMIT 2043 Web Systems and Technologies
Chapter 6 Cookies and Sessions Slide 16
View a product’s
details.
16
AMIT 2043 Web Systems and Technologies
Chapter 6 Cookies and Sessions Slide 17
Customer
can
continue to
shop.
17
AMIT 2043 Web Systems and Technologies
Chapter 6 Cookies and Sessions Slide 18
18
AMIT 2043 Web Systems and Technologies
Chapter 6 Cookies and Sessions Slide 19
19
AMIT 2043 Web Systems and Technologies
Chapter 6 Cookies and Sessions Slide 20
References
• PHP 6 and MySQL 5 by Ullman, L. Peachpit Press
• PHP Programming with MySQL Second Edition
by Gosselin, D., Kokoska, D. & Easterbrooks, R.
Course Technology
20
AMIT 2043 Web Systems and Technologies