0% found this document useful (0 votes)
38 views3 pages

Exp 10

The document describes an experiment to create and observe session and persistent cookies. It defines cookies as small text files stored on a user's device by a web server to store information. Session cookies last until the browsing session ends, while persistent cookies expire at a specified date. The document provides code samples to create session and persistent cookies, read existing cookie values, and a sample program combining cookie creation and reading. It concludes the experiment successfully demonstrated creating, deleting, and manipulating cookies and observing their behavior under different browser settings.

Uploaded by

labiot324
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)
38 views3 pages

Exp 10

The document describes an experiment to create and observe session and persistent cookies. It defines cookies as small text files stored on a user's device by a web server to store information. Session cookies last until the browsing session ends, while persistent cookies expire at a specified date. The document provides code samples to create session and persistent cookies, read existing cookie values, and a sample program combining cookie creation and reading. It concludes the experiment successfully demonstrated creating, deleting, and manipulating cookies and observing their behavior under different browser settings.

Uploaded by

labiot324
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/ 3

Experiment No.

10
Aim: Develop a Web page for creating session cookies and persistent cookies. Observe the effects with
browser cookie settings.

Theory:
Cookies are data stored in small text file that a web site server writes on client’s hard disk. Cookies are created by the
web server when a requested web page is loaded on client’s web browser. Cookie can be used to store information
(plain text) in a text file based on the application’s requirement.

Types of cookie:
1. Session cookie: It resides in client’s memory till the session is active. A browser session begins when user
request for a website on browser and ends when user closes the web site. When user is working with a web
site his session is active and for this period a cookie created by server will remain in user’s PC.

2. Persistent cookie: It is a cookie with assigned expiry date. When user request for a web site on a browser, a
cookie is created by server and stored on client’s machine. This cookie is created with expiry date so the
cookie resides on client’s machine till its expiry date. The cookie may reside on client’s machine for few
hours, days, months and years.

Cookies are a plain text data record of 5 variable-length fields −

 Expires − The date the cookie will expire. If this is blank, the cookie will expire when the visitor
quits the browser.
 Domain − The domain name of your site.
 Path − The path to the directory or web page that set the cookie. This may be blank if you want to
retrieve the cookie from any directory or page.
 Secure − If this field contains the word "secure", then the cookie may only be retrieved with a secure
server. If this field is blank, no such restriction exists.
 Name=Value − Cookies are set and retrieved in the form of key-value pairs. The text stored inside a
cookie file contains information in the form name-value pair which stores name of the field and its
value. For example, if username is the field name then xyz can be a value stored inside it. Semicolon,
comma, white space is not allowed in the name of cookie. To use these characters, escape character
(\) is used before them. The escape character tells the browser that the semicolon, comma or white
space is part of the name or value and not a special character.

Each cookie has four parts: name, assignment operator, value and semicolon. Semicolon is a delimiter. It is a
character that indicates end of cookie.

Creating session cookie:


A cookie can be created by assigning a string value to the document.cookie object.
For example: document.cookie = "key1 = value1;key2 = value2;expires = date";
Example:
function WriteCookie()
{
if( document.myform.customer.value == "" )
{
alert("Enter some value!");
return;
}
cookievalue = escape(document.myform.customer.value) + ";";
document.cookie = "name=" + cookievalue;
document.write ("Setting Cookies : " + "name=" + cookievalue );
}

Creating persistent cookie:


A cookie is created as a persistent cookie by setting an expiry date for it at the time of creation.
Example:
function WriteCookie()
{
var now = new Date();
now.setMonth( now.getMonth() + 1 );
cookievalue = escape(document.myform.customer.value) + ";"
document.cookie = "name=" + cookievalue;
document.cookie = "expires=" + now.toUTCString() + ";"
document.write ("Setting Cookies : " + "name=" + cookievalue );
}

Reading cookie values from user system:


After creating a cookie,it is stored in user’s computer memory. To read values stored in cookie,
document.cookies collection is used.
Example:
function ReadCookie()
{
var allcookies = document.cookie;
document.write ("All Cookies : " + allcookies );
cookiearray = allcookies.split(';');
for(var i=0; i<cookiearray.length; i++)
{
name = cookiearray[i].split('=')[0];
value = cookiearray[i].split('=')[1];
document.write ("<br>Key is : " + name + " and Value is : " + value);
}
}

Programs: Write your own program.


Sample program:
<html>
<head>
<script type = "text/javascript">
function WriteCookie()
{
var now = new Date();
now.setMonth( now.getMonth() + 1 );
cookievalue = escape(document.myform.customer.value) + ";"
document.cookie = "name=" + cookievalue;
document.cookie = "expires=" + now.toUTCString() + ";"
}
function ReadCookie()
{
window.alert("hi");
var allcookies = document.cookie;
document.write ("All Cookies : " + allcookies );

cookiearray = allcookies.split(';');
for(var i=0; i<cookiearray.length; i++)
{
name = cookiearray[i].split('=')[0];
value = cookiearray[i].split('=')[1];
document.write ("<br>Key is : " + name + " and Value is : " + value);
}
}
</script>
</head>
<body>
<form name = "myform" action = "">
Enter name: <input type = "text" name = "customer"/>
<input type = "button" value = "Set Cookie" onclick = "WriteCookie()"/>
<input type="button" value="Read Cookie" onclick="ReadCookie()"/>
</form>
</body>
</html>

Practical Related Question(observation):


1. Give the storage location of cookies on user’s system.
Ans.
2. Difference between session and persistent cookies.
Ans.
3. How to view cookies stored on user’s system?
Ans.
4. How to delete cookie from system?
Ans.
5. How to block cookies storage on user’s system?
Ans.

Conclusion:
With all the concepts based on cookies creation,deletion and manipulation , successfully executed all
programs with correct output.

You might also like