Vous êtes sur la page 1sur 16

Web Engineering‐II  

Using ASP.net 

By
Adnan Amin
Lecturer / Software programmer
Information and Communication Technology (ICT-Dept)
www.geoamins.com
Why cookies.
• Cookies seem to solve the problem of moving data
from page to page.

• Cookies can be stored so that it remains there after


the user leaves your site and will still be available
when the user entered your web site a hour, day, or
month later.

• Cookies were originally designed for storing small


amounts of information for short periods of time.

By: Adnan Amin


2
(Lecturer/Programmer)
Introduction to cookies 
• The place (file) which can store small amounts of
information that containing variable=value pairs.

• The user’s browser stores cookies on the user’s


computer.

• The stored data can use again and again until the
cookies is available on the user’s computer.

• Cookies should be used only to store non-sensitive


information, or information that can be retrieved from
an authoritative source. Cookies shouldn’t be trusted.

By: Adnan Amin


3
(Lecturer/Programmer)
Limitations of cookies 
• Cookies are not under your control. They are under the user’s
control.
• The user can at any time delete the cookie.
• Users can set their browsers to refuse to allow any cookies.

• Many users do refuse cookies or routinely delete them.

• Many users are not comfortable with the idea of a stranger


storing things on their computers.

• Cookie size is limited to 4096 bytes. It is not much, so cookies


are used to store small amounts of data, often just user id.

• Also, number of cookies is limited to 20 per website. If you


make new cookie when you already have 20 cookies, browser
will delete oldest one
By: Adnan Amin
4
(Lecturer/Programmer)
Remember
• When you store information in cookies,
remember that it’s quite different from
storing data in the Session object:
To see where IE 7 & 8 stores its Cookies in Windows Vista / 7 :  
  
Open Explorer > Organize > Folder Options > Views > Check ʹDo not show 
hidden files and foldersʹ and Uncheck ʹHide protected OS filesʹ > Apply > OK. 

Now you will be able to see the two real locations of Vistaʹs Cookies folders. 
  
C:\Users\username\AppData\Roaming\Microsoft\Windows\Cookies  

By: Adnan Amin


5
(Lecturer/Programmer)
Cookies Terminologies 
• READING COOKIES
• Read cookies using Request.Cookies
collection
o Request is actually an HTTPRequest object
o Each object is a HTTPCookiesCollection consisting of
HTTPCookie objects.

• WRITING COOKIES
• Response is actually an HTTPResponse object.

By: Adnan Amin


6
(Lecturer/Programmer)
Create an instance of a cookie then 
add it to the collection. 
• Dim cookie As HttpCookie = New HttpCookie("myCookie")
• cookie.Value = Me.TextBox1.Text
• cookie.Expires = DateTime.Now.AddDays(1)
• Response.Cookies.Add(cookie)

By: Adnan Amin


7
(Lecturer/Programmer)
Create directly cookie for 
the same user. 
• Response.Cookies("UserID").Value = TextBox1.Text
• Response.Cookies("UserID").Expires =
DateTime.Now.AddDays(1)

By: Adnan Amin


8
(Lecturer/Programmer)
Create an instance of a cookie, 
then add two subkeys, and add 
it to the collection. 
• Dim cookie1 As HttpCookie = New
HttpCookie("UserData")
• cookie1.Values("LastVisit") =
DateTime.Now.ToString()
• cookie1.Values("UserID") = TextBox1.Text
• cookie1.Expires = DateTime.Now.AddDays(1)
• Response.Cookies.Add(cookie1)

By: Adnan Amin


9
(Lecturer/Programmer)
Read a cookie by creating 
an instance of the cookie. 
• If Not Request.Cookies("myCookie") Is Nothing Then
• Dim cookie As HttpCookie =
Request.Cookies("myCookie")
• Label2.Text = cookie.Value
• Else
• Label2.Text = "the cookie is not found"
• End If

By: Adnan Amin


10
(Lecturer/Programmer)
Read a cookie value directly, 
encoding it for safety. 
• If Not Request.Cookies("UserID") Is Nothing Then
• Label3.Text = Request.Cookies("UserID").Value
• End If

By: Adnan Amin


11
(Lecturer/Programmer)
Read a subkey value in a 
cookie directly. 
• If Not Request.Cookies("UserData") Is Nothing Then
• Label4.Text = Request.Cookies("UserData")("UserID")
• Label5.Text = Request.Cookies("UserData")("LastVisit")
• End If

By: Adnan Amin


12
(Lecturer/Programmer)
Delete a cookie by setting 
the expiration to a 
previous date/time. 

• Response.Cookies(“myCookie").Expires =
DateTime.Now.AddDays(-1)

By: Adnan Amin


13
(Lecturer/Programmer)
How to check does visitorʹs 
web browser supports cookies 
• If Request.Browser.Cookies Then
' Cookies supported
Else
' Web browser not supports cookies
End If

By: Adnan Amin


14
(Lecturer/Programmer)
Delete a Subkey value by 
removing it. 

Response.Cookies("UserData").Values.Remove("LastVisit")

By: Adnan Amin


15
(Lecturer/Programmer)
Thank You! 

By: Adnan Amin


16
(Lecturer/Programmer)

Vous aimerez peut-être aussi