0% found this document useful (0 votes)
16 views4 pages

Chapter 4.1

Uploaded by

Arya M
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)
16 views4 pages

Chapter 4.1

Uploaded by

Arya M
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/ 4

Chapter 4

Cookies:
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. A JavaScript can be
used to create cookies and perform various operations on it such as reading, deleting, etc. cookie can be used
to store information (plain text) in a text file based on the application’s requirement. Cookie may be used to
store user ID and password data when a user login to the web site. Some applications may store date and
time of last access inside a cookie.

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.

A web server sends some data to the client’s browser in the form of a cookie. The browser may accept the
cookie. If it does, it is stored as a plain text record on the visitor's hard drive. When the visitor arrives at
another page on your site, the browser sends the same cookie to the server for retrieval. Once retrieved, your
server knows/remembers what was stored earlier.
Cookies are a plain text data record of 5 variable-length fields −
EDPSN
 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.
Cookies were originally designed for CGI programming. The data contained in a cookie is automatically
transmitted between the web browser and the web server, so CGI scripts on the server can read and write
cookie values that are stored on the client. Each cookie contains address of server that creates it. Due to this
only web page from particular server can only read it. Information/data stored in a cookie identifies the
computer that was used to visit the web site.
JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can
read, create, modify, and delete the cookies that apply to the current web page.

Creating cookie:
Each cookie has four parts: name, assignment operator, value and semicolon. Semicolon is a delimiter. It is a
character that indicates end of cookie.
A cookie can be created by assigning a string value to the document.cookie object.
Example: document.cookie = "key1 = value1;key2 = value2;expires = date";
Program:
<html>
<head>
<script type="text/javascript">
function writecookie()
{
with(window.document.forms.userentry)
{
if(cname.value=="")
{
alert("Enter the value");
}
var ck=cname.value;
document.cookie= "customername=" + ck +";";
document. write ("setting cookie" + "name=" + ck);
}
}
</script>
</head>
<body>
<form name="userentry">
<input type="text" name="cname">
<input type="button" value="set cookie" onclick="writecookie()">
</form>
</body>
</html>

In the above program, when customer enters name in a textbox and click on button, writecookie function is
called to create a cookie with customername.

Output:
Reading Cookies:
The value stored in window.document.cookie object is the cookie with user information. When a browser
executes window.document.cookie statement in a JavaScript,the browser copies the cookie to the cookie
object. The document.cookie string keep a list of name=value pairs separated with semicolons, where name
is the name of a cookie and value is the string stored in cookie.Then this object can be used anytime when
you want to access the cookie. You can use strings' split() function to break a string into key and values.
Example
<html>
<head>
<script type = "text/javascript">
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);
}
}
</script>
</head>
<body>
<form name = "myform" action = "">
<p> click the following button and see the result:</p>
<input type = "button" value = "Get Cookie" onclick = "ReadCookie()"/>
</form>
</body>
</html>

Deleting a cookie:

Cookies are automatically deleted when either the browser session ends or its expiry date has been reached.
Life of a cookie can be extended by setting a expiry date with expires attribute.
A date is stored in a variable of a date data type.
1. getMonth()- It returns the current month based on the system clock of computer running the script.
2. setMonth()- It is used to assign the month to the date variable.
3. toGMTString()- This method returns the value of the date variable to a string which is assigned to the
cookie.
<html> Updation Cookie
<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() + ";"
document.write ("Setting Cookies : " + "name=" + cookievalue );
}
</script>
</head>
<body>
<form name = "myform" action = "">
Enter name: <input type = "text" name = "customer"/>
<input type = "button" value = "Set Cookie" onclick = "WriteCookie()"/>
</form>
</body>
</html>

To delete a cookie immediately after creating it need to set the expiry date to a time in the past. For example,
delete a cookie by setting its expiry date to one month behind the current date.

<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() + ";"
document.write("Setting Cookies : " + "name=" + cookievalue );
}
</script>
</head>
<body>
<form name = "myform" action = "">
Enter name: <input type = "text" name = "customer"/>
<input type = "button" value = "Set Cookie" onclick = "WriteCookie()"/>
</form>
</body>
</html>

You might also like