We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 50
| Cookies and
CHAPTER 4 Browser Data
syllabus
41 Cookies — basic of cookies, reading a cookie value, writing a
cookie value, creating a cookies, deleting a cookies, setting the
expiration date of cookie.
42 Browser — opening a window, giving the new window focus, |
window position, changing the content of window, closing a
window, scrolling a web page, multiple windows at once, creating a
page in new window, JavaScript in URL's, JavaScript security,
Timers, Browser location and history.
1 4.1 COOKIES
6Q. What is cookie? 8
62. List and explain types of cookies.
State the use of cookies,
How cookies works explain with diagram.
Describe how to read cookie value and write a cookie value. Explain
with example.
Basic of Cookies
A ser, 1
“okie is a piece of data that a server sends to a web browser. The
Ww . ¢ ci ki
..., P'OWSer then stores it in the user’s computer and sends the cookie
ack
‘0 the same server in the next requests,Client Side Scripting Language (MSBTE:
=
There are three types of cookies:
1.
2.
3.
(New Syll wef academic year 19-20) (D5-16A) mane publicat™
(Cookies and Browser Data) Pg
(be
A cookie is an amount of information that persists between a a
and a client-side. A web prowser stores this information at the i Side
browsing me
A cookie contains the information as @ string generally in the form
name-value pait separated by semi-colons. fy
Tt maintains the state of a user and remembers the user's informati,,
among all the web pages.
The server uses cookies for identifying if two successive requests .,,..
from the same web browser.
Types of cookies
First Party Cookies : These cookies are created by your website ani
can only be read by your website.
Third-party cookies : These cookies are produced by third-pary
advertising on your website. These cookies can only be read on any site
that displays the same ad using the advertising code.
Session cookies : These cookies are saved on your browser. They are
destroyed when the browser is closed.
Use of cookies
Session management : cookies allow you to manage any information
that the server should remember. For example, logins, shopping ca
etc.
Personalization : cookies allow you to store user preferences, themes
and setting specific to a user.
Tracking : cookies help record and analyze user behaviors ¥
advertising.
How Cookies Works?
When @ user sends a request to the server, then each of that reques®”
uealed as a new request sent by the different user.
: e
So, to recognize the old user, we need to add the cookie wil"
response from the server.
er: Language (MSBTE) Cookies and Browser Data) Pg, no. (4-3)
i scripting
1 5
enever @ user sends a request from browser (client-side) to the
ye w
Ne
the cookie is added with that request automatically and due to the
gcrvel> the server recognizes the users,
sokies EE
Request
_—_»
Response + Cookie
WEB —_—__
BROWSER
Request + Cookie
_——_—>,
tw
Fig. 4.1.1 : Process of Cookie
Web Browsers and Servers use HTTP Protocol to communicate and
HTTP is a stateless Protocol. But for a commercial website, it is required
‘o maintain session information among different pages.
For €xample, one user registration ends after, completing many pages.
But how to maintain users' session information across all the web pages.
Cookies are a plain text data record of 5 variable-length fields:
1,
Expires ~ the date the cookie will expire. If this is blank, the
, “okie will expire when the visitor quits the browser.
x Domain ~ the domain name of your site.
fe ~ the path to the directory or web page that set the cookie.
his may be blank if we want to retrieve the cookie from any
f directory or page.
Secure — 1¢ this field contains the word "secure", then the cookie
ney nly be retrieved with a secure server. If this field is blank, no
“SY, yy Ty
‘ef academic year 19-20) (D5-16A) ‘ech-Neo Publications.oN
Client Side Scripting Language (MSBTE) (Cookies and Browser Data) Pg, ,,
Fo. (44
)
5, Name=Value - Cookies are set and retrieved in the form of
value pairs key,
e Cookies were originally designed for CGI programming. The i
ed in a cookie is automatically transmitted between the =
‘Web,
contain
browser and the web server, 50 CGI scripts on the server can reag
, and
ed on the client.
write cookie values that are stor
.o manipulate cookies using the cookie property of th
le
e JavaScript can als
read, create, modify, and delete 4,
e
Document object. JavaScript can
cookies that apply to the current web page.
Ya 4.1.2 Reading A Cookie Value
e Reading a cookie is just as simple as writing one, because the value of
e cookie. So we can use this string
the document.cookie object is the
whenever we want to access the cookie.
nt.cookie string will keep a list of name=value pairs
e The documel
where name is the name of a cookie and value
separated by semicolons,
is its string value.
e The split() function is use to break a string into key and values.
Example
Cookie Examy
‘