Lesson_12 Cookies

download Lesson_12 Cookies

of 16

Transcript of Lesson_12 Cookies

  • 8/7/2019 Lesson_12 Cookies

    1/16

    WebEngineeringII UsingASP.net

    By

    Adnan AminLec turer / Softw are p rog rammer

    Information and Comm unica tion Tec hnology (ICT-Dep t)

    www.geoamins.com

  • 8/7/2019 Lesson_12 Cookies

    2/16

    Why cookies. Cookies seem to solve the prob lem 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 ava ilablewhen the user entered your web site a hour, day, or

    month later.

    Cookies were originally designed for storing smallamounts of information for short periods of time.

    By: Adna n Am in(Lecturer/Programmer)

    2

  • 8/7/2019 Lesson_12 Cookies

    3/16

    Introduction

    to

    cookies

    The place (file) which can store small amounts ofinformation that containing variable=value pairs. The users browser stores cookies on the userscomputer. The stored data can use again and again until thecookies is available on the users computer. Cookies should be used only to store non-sensitive

    information, or information that can be retrieved froman authoritative sourc e. Cookies shouldnt be trusted.

    By: Adna n Am in(Lecturer/Programmer)

    3

  • 8/7/2019 Lesson_12 Cookies

    4/16

    Limitationsof

    cookies

    Cookies are not under your control. They are under the userscontrol.

    The user can at any time delete the c ookie.

    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 strangerstoring things on their computers.

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

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

    Also, number of cookies is limited to 20 per website. If youmake new c ookie when you already have 20 cookies, browserwill delete oldest one

    By: Adna n Am in(Lecturer/Programmer)

    4

  • 8/7/2019 Lesson_12 Cookies

    5/16

    Remember

    When you store information in cookies,remember that its quite different from

    storing data in the Session object:

    By: Adna n Am in(Lecturer/Programmer)

    5

    ToseewhereIE7&8storesitsCookiesinWindowsVista/7:

    OpenExplorer>Organize>FolderOptions>Views>Check Donotshowhiddenfilesandfolders andUncheck HideprotectedOSfiles >Apply>OK.

    NowyouwillbeabletoseethetworeallocationsofVistasCookiesfolders.

    C:\Users\username\AppData\Roaming\Microsoft\Windows\Cookies

  • 8/7/2019 Lesson_12 Cookies

    6/16

    CookiesTerminologies

    READING COOKIES

    Read cookies using Request.Cookiescollectiono Request is ac tua lly an HTTPRequest objec t

    o Each objec t is a HTTPCookiesCollec tion consisting ofHTTPCookie objec ts.

    WRITING COOKIES Response is ac tua lly an HTTPResponse objec t.

    By: Adna n Am in(Lecturer/Programmer)

    6

  • 8/7/2019 Lesson_12 Cookies

    7/16

    Createaninstanceofacookiethen

    addittothecollection.

    Dim cookie As HttpCookie = New HttpCookie("myCookie")

    cookie.Value = Me.TextBox1.Text

    cookie.Exp ires = DateTime.Now.AddDays(1)

    Response.Cookies.Add(cookie)

    By: Adna n Am in(Lecturer/Programmer)

    7

  • 8/7/2019 Lesson_12 Cookies

    8/16

    Createdirectlycookieforthesameuser.

    Response.Cookies("UserID").Va lue = TextBox1.Text

    Response.Cookies("UserID").Expires =DateTime.Now.AddDays(1)

    By: Adna n Am in(Lecturer/Programmer)

    8

  • 8/7/2019 Lesson_12 Cookies

    9/16

    Createaninstanceofacookie,

    thenaddtwosubkeys,andaddittothecollection.

    Dim c ookie1 As HttpCookie = NewHttpCookie("UserData")

    cookie1.Values("LastVisit") =DateTime.Now.ToString()

    cookie1.Values("UserID") = TextBox1.Text

    cookie1.Exp ires = DateTime.Now.AddDays(1) Response.Cookies.Add(cookie1)

    By: Adna n Am in(Lecturer/Programmer)

    9

  • 8/7/2019 Lesson_12 Cookies

    10/16

    Readacookiebycreating

    aninstanceofthecookie.

    If Not Request.Cookies("myCookie") Is Nothing Then Dim cookie As HttpCookie =

    Request.Cookies("myCookie")

    Label2.Text = cookie.Value

    Else

    Label2.Text = "the c ookie is not found"

    End If

    By: Adna n Am in(Lecturer/Programmer)

    10

  • 8/7/2019 Lesson_12 Cookies

    11/16

    Reada

    cookie

    value

    directly,

    encodingitforsafety.

    If Not Request.Cookies("UserID") Is Nothing Then

    Label3.Text = Request.Cookies("UserID").Value

    End If

    By: Adna n Am in(Lecturer/Programmer)

    11

  • 8/7/2019 Lesson_12 Cookies

    12/16

    Readasubkey valueinacookiedirectly.

    If Not Request.Cookies("UserData") Is Nothing Then Label4.Text = Request.Cookies("UserData")("UserID")

    Label5.Text = Request.Cookies("UserData")("LastVisit")

    End If

    By: Adna n Am in(Lecturer/Programmer)

    12

  • 8/7/2019 Lesson_12 Cookies

    13/16

    Delete

    a

    cookie

    by

    setting

    theexpirationtoa

    previousdate/time.

    Response.Cookies( myCookie").Exp ires =DateTime.Now.AddDays(-1)

    By: Adna n Am in(Lecturer/Programmer)

    13

  • 8/7/2019 Lesson_12 Cookies

    14/16

    Howtocheckdoesvisitors

    webbrowsersupportscookies If Request.Browser.Cookies Then

    ' Cookies supportedElse' Web browser not supports c ookies

    End If

    By: Adna n Am in(Lecturer/Programmer)

    14

  • 8/7/2019 Lesson_12 Cookies

    15/16

    DeleteaSubkey

    value

    by

    removingit.

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

    By: Adna n Am in(Lecturer/Programmer)

    15

  • 8/7/2019 Lesson_12 Cookies

    16/16

    ThankYou!

    By: Adna n Am in(Lecturer/Programmer)

    16