CP476 Internet Computing CGI1 Cookie –Cookie is a mechanism for a web server recall info of...

4
CP476 Internet Computing CGI 1 Cookie Cookie is a mechanism for a web server recall info of accessing of a client browser A cookie is an object sent by the server to the client while the client access the pages in the server Cookies are created by some software system on the server (maybe a CGI program). A cookie is embedded in the header of a HTML At the time a cookie is created, it is given a lifetime Every time the browser sends a request to the server that created the cookie, while the cookie is still alive, the cookie is included The server can retrieve the information embedded in the cookie. Lecture 12. Cookies and Sessions Objective: to learn how to use cookies to store access information on client side, and how to use for session keeping.

Transcript of CP476 Internet Computing CGI1 Cookie –Cookie is a mechanism for a web server recall info of...

Page 1: CP476 Internet Computing CGI1 Cookie –Cookie is a mechanism for a web server recall info of accessing of a client browser –A cookie is an object sent by.

CP476 Internet Computing CGI

1

• Cookie

– Cookie is a mechanism for a web server recall info of accessing of a client browser

– A cookie is an object sent by the server to the clientwhile the client access the pages in the server

– Cookies are created by some software system on the server (maybe a CGI program). A cookie is embedded in the header of a HTML

– At the time a cookie is created, it is given a lifetime

– Every time the browser sends a request to the server that created the cookie, while the cookie is still alive, the cookie is included

– The server can retrieve the information embedded in the cookie.

Lecture 12. Cookies and Sessions

Objective: to learn how to use cookies to store access information on client side, and how to use for session keeping.

Page 2: CP476 Internet Computing CGI1 Cookie –Cookie is a mechanism for a web server recall info of accessing of a client browser –A cookie is an object sent by.

CP476 Internet Computing CGI

2

• CGI.pm includes support for cookies

cookie(-name => a_cookie_name,

value => a_value,

expires => a_time_value);

The name can be any string

The value can be any scalar value

The time is a number followed by a unit code

(d, s, m, h, M, y)

See example code

Page 3: CP476 Internet Computing CGI1 Cookie –Cookie is a mechanism for a web server recall info of accessing of a client browser –A cookie is an object sent by.

CP476 Internet Computing CGI

3

• Cookies must be placed in the HTTP header at the time the header is created

header(-cookie => $my_cookie);

• To fetch the cookies from an HTTP request, call cookie with no parameters

A hash of all current cookies is returned

To fetch the value of one particular cookie, send the cookie’s name to the cookie function

$age = cookie(′age′);

See example code

• Use cookie for authentication

• Remember the password for later access of a page which needs authentication

• Limited time for accessing a page after it is authorized

Page 4: CP476 Internet Computing CGI1 Cookie –Cookie is a mechanism for a web server recall info of accessing of a client browser –A cookie is an object sent by.

CP476 Internet Computing CGI

4

• A session is the collection of all of the requests made by a particular browser from the time the browser is started until the user exits the browser

– The HTTP protocol is stateless. But, there are several reasons why it is useful for the server to relate a request to a session

• Shopping carts for many different simultaneous customers

• Customer profiling for advertising

• Customized interfaces for specific clients

• Approaches to storing client information:

• Store it on the server – too much to store!

• Store it on the client machine - this works

– Cookie provide a method to keep a session

– Example: for shopping cart, every time a shopper add an item into his shopping card, the server add a cookie about the item to the client browser, when the shopper check out, the web server get all the cookies saved on the client browser. In such way, the server don’t have store any information selected by the shopper before he/she do the final check out.