What are Cookies? Websites use cookies to store user-specific information Cookies are stored on the hard disk of the Web browser User information and preferences are send between We
Trang 1Working with Cookies
Session 16
Trang 2 Database APIs allows the developers
to write applications that are
easily accessible between the
database products
accessing data from the database
server
Review - I
Trang 5What are Cookies?
Websites use cookies to store
user-specific information
Cookies are stored on the hard
disk of the Web browser
User information and preferences
are send between Web browser and
Web server
Trang 6Static Web Pages
When a Web browser requests for a static Web page, the server responds by sending the Web page the browser requested for
No user interaction
Trang 7Dynamic Web Pages
and record it for further processing
Trang 8Types of Cookies
Persistent - Remains stored in
the Web browser for a period
specified during the time of it’s
creation
Non-persistent - Deleted from the
Web browser as soon as the user
closes the browser
Trang 9Common Uses of Cookies
Enable websites determine:
Number of times the user has visited the website
Number of new visitors
Number of regular users
Frequency of a visitor visiting the website
Store the date that a user last visited the website
Maintain a customized Web page settings for a user
Trang 10Working with Cookies
Web servers and Web browsers send
each other cookies in HTTP
headers
Web server sends the cookie in a
Set-Cookie header field
Set-Cookie is a part of HTTP
response
Trang 11Setting a Cookie
Refers to sending the cookie to the browser
PHP uses two functions to set a
cookie, such as:
setcookie(): Sends cookie with urlencoding
setrawcookie(): Sends cookie without
urlencoding
Trang 12setcookie() Function
Generates a correct header field
that is sent along with the rest of the header information
Syntax:
setcookie(name, value, expire, path, domain, secure)
Trang 13Example of setcookie() Function
setcookie("mycookie", $mapname,
time()+86400, "/webmap/", ".webworldmaps.com");
Where,
mycookie - Name of the cookies
time()+3600 - Time when the cookie will expire
/webmap – Path where the cookie will be stored
Trang 14Retrieving Cookie Value
PHP provides three ways of
retrieving a cookie value:
Passing a variable as cookie name
Using $_COOKIE[]
Using $HTTP_COOKIE_VARS[]
Trang 15Passing Variable as Cookie Name
Use the variable as the cookie
name
Syntax:
echo $cookie_name
PHP searches all the variables
present in the client computer
Works only when the
Trang 16Using $_COOKIE[]
Requires PHP 4.1
Simpler and more secured than
using the $HTTP_COOKIE_VARS[]
Syntax:
$_COOKIE[‘$cookie_name’]
Trang 17Using $HTTP_COOKIE_VARS[]
Global variable that reads a value
of the cookie
PHP passes the name of the variable
as the key to the
$HTTP_COOKIE_VARS[] array
Syntax:
$HTTP_COOKIE_VARS[$cookie_name];
Trang 18Deleting a Cookie - I
Similar to setting a cookie
Two ways to delete a cookie:
Resetting the expiry time of the cookie to a
time in the past
Resetting the cookie by specifying the name of
the cookie
Trang 19Deleting a Cookie - II
Example to delete a cookie with a
date in the past:
setcookie ("$cookie_name", "",
time()-8000);
Example to delete Cookie by
specifying the name of the cookie:
setcookie($cookie_name);
Trang 20Drawbacks of Cookies - I
Cookies are not considered secured and reliable
Some of the drawbacks of cookies:
Cookies cannot contain more than a certain amount of
Trang 21Drawbacks of Cookies - II
Some users disable cookies while accessing
websites
There can be multiple persons using the same
computer visiting the same website
Cookies need to be called on each Web page
Trang 22Summary - I
user-specific information
browser’s hard disks
send to and from the Web browser
Persistent
Trang 23Summary - II
PHP provides three ways of
retrieving a cookie value:
Passing a variable as cookie name
Using $_COOKIE[]
Using $HTTP_COOKIE_VARS[]
PHP uses two functions to set a
cookie, such as
Trang 24Summary - III
Two ways to delete a cookie:
Drawbacks of cookies: