PC Review


Reply
Thread Tools Rate Thread

Cookies Count

 
 
rn5a@rediffmail.com
Guest
Posts: n/a
 
      28th Sep 2007
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

 
Reply With Quote
 
 
 
 
Juan T. Llibre
Guest
Posts: n/a
 
      28th Sep 2007
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/
======================================
<(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
rn5a@rediffmail.com
Guest
Posts: n/a
 
      29th Sep 2007
On Sep 27, 10:48 pm, "Juan T. Llibre" <nomailrepl...@nowhere.com>
wrote:
> 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/
> ======================================
>
>
>
> <r...@rediffmail.com> wrote in messagenews:(E-Mail Removed)...
> > 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- Hide quoted text -

>
> - 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Give Request.Cookies and Response.Cookies is there any reason to use another method to use cookies? _Who Microsoft ASP .NET 7 18th Sep 2008 08:49 PM
Me.Context.Request.Cookies value doesn't match what's in cookies file Eric Microsoft ASP .NET 1 28th Dec 2007 09:02 PM
Deleting Cookies (Cookies vs. Temporary Internet File folders) Blithe Windows XP Internet Explorer 9 6th Jul 2004 07:27 PM
Cookies Problem: C:\Documents and Settings\%Username%\Cookies =?Utf-8?B?QWxleGlzIEFsZXhhbmRyb3U=?= Windows XP Internet Explorer 0 21st Jun 2004 05:55 AM
Enable Cookies. Web browser options set to disable cookies and hotmail Sharon Windows XP Internet Explorer 1 13th Sep 2003 01:40 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:53 AM.