Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

Cookies Count

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rn5a@rediffmail.com - 28 Sep 2007 02:37 GMT
This is how I am creating & then reading cookies:

<script runat="server">
   Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
       'create cookies
       Response.Cookies("UserName").Value = "Ron"
       Response.Cookies("UserName").Expires = "12/31/2008"

       Response.Cookies("UserDetails")("FirstName") = "Ronnie"
       Response.Cookies("UserDetails")("LastName") = "Nathan"
       Response.Cookies("UserDetails")("LastVisitedDate") =
DateTime.Now.ToString("d")
       Response.Cookies("UserDetails")("LastVisitedTime") =
DateTime.Now.ToString("T")
       Response.Cookies("UserDetails").Expires = "12/31/2008"

       'read cookies count
       Response.Write(Request.Cookies.Count & " cookies created!
<br>")

       'read cookies
       Response.Write("Full Name : " & Request.Cookies("UserDetails")
("FirstName") & " " & Request.Cookies("UserDetails")("LastName") &
"<br>")

       Response.Write("User Name : " &
Request.Cookies("UserName").Value & "<br>")

       Response.Write("Last Visit: " & Request.Cookies("UserDetails")
("LastVisitedDate") & " at " & Request.Cookies("UserDetails")
("LastVisitedTime"))
   End Sub
</script>

As such, the cookie gets created successfully. ONLY 1 cookie gets
created but when I run the above code for the first time i.e. when the
cookie doesn't exist, Request.Cookies.Count evaluates to 3 & on
further page refreshes, Request.Cookies.Count evaluates to 5.

How is Request.Cookies.Count evaluating to 3 & 5 when ONLY 1 cookie
gets created (in the Temporary Internet Files folder)?

Thanks
Juan T. Llibre - 28 Sep 2007 04:48 GMT
re:
!> How is Request.Cookies.Count evaluating to 3 & 5

Request.Cookies.Count doesn't only count the cookies you have explicitly created in the page.
It also counts the ASP.NET cookie(s).

re:
!> when ONLY 1 cookie gets created (in the Temporary Internet Files folder)?
You're actually creating 5 cookies, and storing them in one cookie file.

You can easily loop through all the cookies, and verify this.

Right after your code line which reads :

Response.Write("Last Visit: " & Request.Cookies("UserDetails")("LastVisitedDate") _
& " at " & Request.Cookies("UserDetails")("LastVisitedTime"))

...place this code :

 Dim output As New System.Text.StringBuilder()
 Dim aCookie As HttpCookie
 Dim i As Integer
 For i = 0 To Request.Cookies.Count - 1
 aCookie = Request.Cookies(i)
 output.Append(("Name = " & aCookie.Name + "<br />"))

 If aCookie.HasKeys Then
       Dim j As Integer
       For j = 0 To aCookie.Values.Count - 1
       Dim subkeyName as string = Server.HtmlEncode(aCookie.Values.AllKeys(j))
       Dim subkeyValue  as string = Server.HtmlEncode(aCookie.Values(j))
       output.Append(("Subkey name = " & subkeyName + "<br />"))
       output.Append(("Subkey value = " & subkeyValue + "<br /><br />"))
       Next j
       Else
       output.Append(("Value = " & Server.HtmlEncode(aCookie.Value) + "<br /><br />"))
       End If
       Next i
       Label1.Text = output.ToString()

---000---

Now, create a form in your html and add a label named Label1...

<html>
<head runat="server">
   <title>Cookie Example</title>
</head>
<body>
<form id="form1" runat="server">
   <div>
     <asp:Label id="Label1" runat="server"></asp:Label>
   </div>
   </form>
</body>
</html>

Now, run your page again.

You will see *all* the cookies you've created, with their subkey names and values.

Count them...and notice that the ASP..NET SessionID cookie is also listed.

You may be able to see cookies created by other applications,
because you did not restrict the cookies' path to the application
in which you're creating the cookies, so you'll see *all* the cookies
which are being created within your default path ( "/" ).

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
> This is how I am creating & then reading cookies:
>
[quoted text clipped - 39 lines]
>
> Thanks
rn5a@rediffmail.com - 29 Sep 2007 22:40 GMT
On Sep 27, 10:48 pm, "Juan T. Llibre" <nomailrepl...@nowhere.com>
wrote:
> re:
> !> How is Request.Cookies.Count evaluating to 3 & 5
[quoted text clipped - 115 lines]
>
> - Show quoted text -

Thank a lot, Juan, for your input. It was indeed very kind of you to
give me such an elaborate & to-the-point answer. Thanks once again but
I still have got a couple of doubts lingering in my mind.

You already had a look at the code I cited in post #1 that creates &
reads cookies. I created another ASP.NET page & created the same
cookie with the same keys in the traditional ASP.NET way (the code in
post #1 was more of ASP):

<script runat="server">
   Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
       'create cookies
       Dim hCookie As HttpCookie

       hCookie = New HttpCookie("UserName")
       hCookie.Value = "Ron"
       hCookie.Expires = "12/31/2008"
       Response.Cookies.Add(hCookie)

       hCookie = New HttpCookie("UserDetails")
       hCookie.Values.Add("FirstName", "Ronnie")
       hCookie.Expires = "12/31/2008"
       Response.Cookies.Add(hCookie)

       hCookie.Values.Add("LastName", "Nathan")
       hCookie.Expires = "12/31/2008"
       Response.Cookies.Add(hCookie)

       hCookie.Values.Add("LastVisitedDate",
DateTime.Now.ToString("d"))
       hCookie.Expires = "12/31/2008"
       Response.Cookies.Add(hCookie)

       hCookie.Values.Add("LastVisitedTime",
DateTime.Now.ToString("T"))
       hCookie.Expires = "12/31/2008"
       Response.Cookies.Add(hCookie)

       'read cookie count
       Response.Write(Request.Cookies.Count & " cookies created!
<br>")

       'read cookies
       Response.Write("Full Name : " & Request.Cookies("UserDetails")
("FirstName") & " " & Request.Cookies("UserDetails")("LastName") &
"<br>")

       Response.Write("User Name : " &
Request.Cookies("UserName").Value & "<br>")

       Response.Write("Last Visit: " & Request.Cookies("UserDetails")
("LastVisitedDate") & " at " & Request.Cookies("UserDetails")
("LastVisitedTime"))
   End Sub
</script>

The above code creates the same cookie with the same keys & values as
the code in post #1 creates but this time, the cookie count comes to 6
& NOT 3 (which was the cookie count in the code in post #1). Now why
this difference in the cookie count although both the codes, if I am
not mistaken, do the same thing?

Note that I executed the above code AFTER manually DELETING the cookie
that the code in post #1 created & vice-versa.

I also added your code snippet in both the codes. The code in post #1
generated this output (this is only the output your code generated):

------------------------------------------------------------
Name: ASP.NET_SessionId
Value: sxgym055kbqw5t55ywa1hrnw

Name: UserName
Value: Ron

Name: UserDetails
Subkey Name: FirstName
Subkey Value: Ronnie
Subkey Name: LastName
Subkey Value: Nathan
Subkey Name: DateLastVisited
Subkey Value: 9/30/2007
Subkey Name: TimeLastVisited
Subkey Value: 2:03:26 PM
------------------------------------------------------------

whereas the code cited in this post generated this output:

------------------------------------------------------------
Name: ASP.NET_SessionId
Value: ckveunmaaokqqm45fjcp2p2x

Name: UserName
Value: Ron

Name: UserDetails
Subkey Name: FirstName
Subkey Value: Ronnie
Subkey Name: LastName
Subkey Value: Nathan
Subkey Name: LastVisitedDate
Subkey Value: 9/30/2007
Subkey Name: LastVisitedTime
Subkey Value: 2:21:23 PM

Name: UserDetails
Subkey Name: FirstName
Subkey Value: Ronnie
Subkey Name: LastName
Subkey Value: Nathan
Subkey Name: LastVisitedDate
Subkey Value: 9/30/2007
Subkey Name: LastVisitedTime
Subkey Value: 2:21:23 PM

Name: UserDetails
Subkey Name: FirstName
Subkey Value: Ronnie
Subkey Name: LastName
Subkey Value: Nathan
Subkey Name: LastVisitedDate
Subkey Value: 9/30/2007
Subkey Name: LastVisitedTime
Subkey Value: 2:21:23 PM

Name: UserDetails
Subkey Name: FirstName
Subkey Value: Ronnie
Subkey Name: LastName
Subkey Value: Nathan
Subkey Name: LastVisitedDate
Subkey Value: 9/30/2007
Subkey Name: LastVisitedTime
Subkey Value: 2:21:23 PM
------------------------------------------------------------

Since I created 4 keys (FirstName, LastName, LastVisitedDate &
LastVisitedTime) in the cookie named "UserDetails", the same cookie
info got repeated 4 times whereas the code in post #1 generated the
cookie info only once though that code also creates the same 4 keys in
the cookie named "UserDetails". Why this difference?

Also I found that if I get rid of one of the keys created in the
cookie "UserDetails" from the code shown in this post, the cookie
count changes from 6 to 5 & the same cookie info gets repeated 3
times. If I delete another key, then the cookie count comes down to 4
(from 5) & the same cookie info gets repeated 2 times so on & so
forth. So this means that Request.Cookies.Count not only adds the no.
of cookies present but also adds the no. of keys created in the
cookies.......

Ron

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.