0% found this document useful (0 votes)
52 views23 pages

Web Based Programming: BCA Semester: II L-19

Uploaded by

niftyelbakyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views23 pages

Web Based Programming: BCA Semester: II L-19

Uploaded by

niftyelbakyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

INSTITUTE OF INFORMATION TECHNOLOGY & MANAGEMENT

Accredited ‘A’ Grade by NAAC &Recognised U/s 2(f) of UGC act


Rated Category `A+’ by SFRC & `A’ by JAC Govt. of NCT of Delhi
Approved by AICTE & Affiliated to GGS Indraprastha University, New Delhi

Web Based Programming


Dr.Ramandeep Kaur
Associate Professor-CS
BCA
Semester: II
L-19
Contents:Cookies- Setting, modifying, deleting, enabling and disabling a cookie

© 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

 Cookies provide a way for a web application


to store information in the user’s web
browser and retrieve it every time the user
request a page.
Or
 Cookies are a mechanism for storing data in
the remote browser and thus tracking or
identifying return users.

© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Introduction

 A cookie is a name/value pair that is stored in a


browser.

 On the server a web application creates a cookie and


sends it to the browser. On the client the browser saves
the cookie and sends it back to the server every time it
accesses a page from that server.

 By default , cookies only last until the user close


his/her web browser. However , cookies can be set to
persist in the user’s browser for up to three years.

© 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

 To allow users to skip login and registration forms


that gather data like username, password, address or
any data.

 To customize pages that display information like


weather report ,sports scores and stock quotations.

 To focus advertising like banner ads that target the


user’s interests.
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Note:

 A common misconception is that cookies are harmful.


Cookies consists only of plain text , they can not directly
modify user’s computer create pop-up ads, generate spam or
steal files.

© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
How to Create a Cookie?

 The setcookie() function is used to set a cookie.


 Note: The setcookie() function must appear BEFORE
the <html> tag.
Syntax
setcookie(name, value, expire, path, domain, secure, httponly);

© 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:

 If we set the $expire parameter to 0 the cookie only exists


until the user closes the browser, this is called a pre-session
cookie.
 If we set the $expire parameter to a date up to three years
from the current date. In that case cookie stays in the
browser until the expiration date this is called a persistent
cookie.

© 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).

 We then retrieve the value of the cookie "user" (using the


global variable $_COOKIE).
 We also use the isset() function to find out if the cookie is
set:

© 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?

 The PHP $_COOKIE variable is used to retrieve a cookie


value.
 In the example below, we retrieve the value of the cookie
named "user" and display it on a page:
<?php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>

© 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?

 If our application deals with browsers that do not support


cookies, we will have to use other methods to pass
information from one page to another in our application.

© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Enable or disable cookies

 To test how our application behaves if a user has cookies


disabled we can disable cookies in our browser.
 To test how our application behaves under normal
circumstances , we can enable cookies in our browser.

© 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

1. Open the tool menu and select the option command


2. Click on the privacy tab.
3. Use the accepts cookies from sites” check box to
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 Internet
Explorer 8

1. open the tools menu and select the internet options


command. 1.

2. Click the privacy tab.


3. Use the slider control to enable or disable cookies. To
disable cookies , set the security level to “Block all
cookies”. To enable cookies , click the default button
to return to default privacy settings

© 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

1. Open the Tools menu and select the Internet Options


command.
2. Click the security tab.
3. If not disabled, click the “Reset all zones to default
level” button.

© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy