Web Based Programming: BCA Semester: II L-19
Web Based Programming: BCA Semester: II L-19
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
What is a Cookie?
A cookie is a small file that the server
embeds on the user's computer. Each time
the same computer requests for a page
with a browser, it will send the cookie too.
With PHP, you can both create and retrieve
cookie values.
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Definition
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Introduction
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Some users disables cookies in their browsers. As a
result we can not always count on all user having their
cookies enabled.
Browsers generally accepts only 20 cookies from each
site and 300 cookies total. In addition they can limit
each cookie to 4 kilobytes.
A cookie can be associated with one or more sub
domain names.
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Uses for cookies
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
How to Create a Cookie?
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Parameters of the setcookie function
Name - This sets the name of the cookie and is stored in an
environment variable called HTTP_COOKIE_VARS. This variable is used
while accessing cookies.
Value -This sets the value of the named variable and is the content
that you actually want to store.
Expiry - This specify a future time in seconds since 00:00:00 GMT on
1st Jan 1970. After this time cookie will become inaccessible. If this parameter
is not set then cookie will automatically expire when the Web Browser is
closed.
Path -This specifies the directories for which the cookie is valid. A
single forward slash character permits the cookie to be valid for all directories.
Domain - This can be used to specify the domain name in very large
domains and must contain at least two periods to be valid. All cookies are only
valid for the host and domain which created them.
Security - This can be set to 1 to specify that the cookie should only
be sent by secure transmission using HTTPS otherwise set to 0 which mean
cookie can be sent by regular HTTP.
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Note:
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Example 1
The following example creates a cookie named "user" with
the value "John Doe". The cookie will expire after 30 days
(86400 * 30).
The "/" means that the cookie is available in entire website
(otherwise, select the directory you prefer).
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() +
(86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?> © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Modify a Cookie Value
<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() +
(86400 * 30), "/"); ?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "'is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
} ?>
</body> </html>
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Delete a Cookie
To delete a cookie, use the setcookie() function with an expiration date in the past:
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 86400);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Check if Cookies are Enabled
<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>
</body>
</html>
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
How to Retrieve a Cookie Value?
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Example: Accessing Cookies with PHP
<html>
<head>
<title>Accessing Cookies with PHP</title>
</head>
<body>
<?php
echo $_COOKIE["name"]. "<br />";
/* is equivalent to */
echo $HTTP_COOKIE_VARS["name"]. "<br />";
echo $_COOKIE["age"] . "<br />";
/* is equivalent to */
echo $HTTP_COOKIE_VARS["name"] . "<br />";
?>
</body>
</html>
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
What if a Browser Does NOT Support
Cookies?
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Enable or disable cookies
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
To enable or disable cookies in Firefox 3.6
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
To enable or disable cookies in Internet
Explorer 8
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
To reset default security settings in in
Internet Explorer 8
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur