Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 Cookie handling (basic)
 New Topic  Topic Locked
 Printer Friendly
Next Page
Author Previous Topic Topic Next Topic
Page: of 3

Tiggerz
Starting Member

45 Posts

Posted - 29 November 2004 :  22:36:10  Show Profile
Ok, I am trying to set and read a cookie in asp using vbscript. The problem is my code doesnt seem to work when I try and update a cookie.

I use the following code at the top of the asp file to set the initial value for the cookie (assuming one doesnt exist)


Response.Cookies( "myCookie" )("field") = "hello"
Response.Cookies( "myCookie" ).Expires = DateTime.Now.AddYears(30)



I can now pick this up later on in the file using the following.


Dim cookText
cookText = Response.Cookies("myCookie")( "field" )
Response.Write( cookText )


I then update the cookie using


Response.Cookies( "myCookie" )("field") = "this is the new value"
Response.Cookies( "myCookie" ).Expires = DateTime.Now.AddYears(30)



My problem is - how the heck do you get it to read that value back when you reload the page.


Also, I note when I try and access the cookie from another page, I get nothing back at all.

Edited by - Tiggerz on 29 November 2004 22:44:17

dayve
Forum Moderator

USA
5820 Posts

Posted - 29 November 2004 :  22:46:34  Show Profile  Visit dayve's Homepage
Dim cookText
cookText = Request.Cookies("myCookie")( "field" )
Response.Write( cookText )

Go to Top of Page

Tiggerz
Starting Member

45 Posts

Posted - 30 November 2004 :  03:46:44  Show Profile
Whats the difference between the response and request. I figure two different collections, but why ?
Go to Top of Page

muzishun
Senior Member

United States
1079 Posts

Posted - 30 November 2004 :  04:55:11  Show Profile  Visit muzishun's Homepage
It's an issue of which direction the information is flowing. Response will set the cookie to the client, while Request gets the value of that cookie for use on your site.

Bill Parrott
Senior Web Programmer, University of Kansas
Co-Owner and Code Monkey, Eternal Second Designs (www.eternalsecond.com)
Personal Website (www.chimericdream.com)
Go to Top of Page

pdrg
Support Moderator

United Kingdom
2897 Posts

Posted - 30 November 2004 :  10:04:43  Show Profile  Send pdrg a Yahoo! Message
it's an HTTP thing - the browser requests a page from the server. the server responds :) Try the RFC's for more details :)
Go to Top of Page

dayve
Forum Moderator

USA
5820 Posts

Posted - 30 November 2004 :  11:02:35  Show Profile  Visit dayve's Homepage
Request Object with cookies: http://www.devguru.com/Technologies/asp/quickref/request_cookies.html
Response Object with cookies: http://www.devguru.com/Technologies/asp/quickref/response_cookies.html


Edited by - dayve on 30 November 2004 18:13:23
Go to Top of Page

Tiggerz
Starting Member

45 Posts

Posted - 30 November 2004 :  17:56:50  Show Profile
umm both those links seem to point to the same thing.


The login I want to do is

At start of site
Read Cookie
If no cookie value then
Write new cookie

On another page
Update cookie

and on a third page
Read cookie


I've found that things like Response.Cookies("mycookies") = "x" do not work as I get a read only property error, so I am a bit dubious as to the documentation that is out there vs the real implementation.

Go to Top of Page

dayve
Forum Moderator

USA
5820 Posts

Posted - 30 November 2004 :  18:19:51  Show Profile  Visit dayve's Homepage
quote:
Originally posted by Tiggerz

umm both those links seem to point to the same thing.


fixed.
quote:
Originally posted by Tiggerz

I've found that things like Response.Cookies("mycookies") = "x" do not work as I get a read only property error, so I am a bit dubious as to the documentation that is out there vs the real implementation.


Extremely Basic Demo: http://www.hafresno.org/testing/page1.asp

page1.asp
<html>
<head>
	<title>Page 1</title>
</head>
<body>
<% Response.Cookies("mycookies") = "x" %><br />
<a href="page2.asp">Page 2</a>
</body>
</html>

page2.asp
<html>
<head>
	<title>Page 2</title>
</head>
<body>
<% =Request.Cookies("mycookies") %><br />
</body>
</html>


another resource for you: http://www.w3schools.com/asp/asp_cookies.asp


Edited by - dayve on 30 November 2004 18:29:18
Go to Top of Page

Tiggerz
Starting Member

45 Posts

Posted - 30 November 2004 :  20:41:35  Show Profile
ok, thanks heaps, I think I have a handle on it now.

When the client sends a 'request' to the server for a web page, it bundles some client information with that 'request', part of this is the collection of cookies related to your website. So when you use 'Request.Cookies' you are accessing this collection.

When the server sends its 'Response' back to the client it sends a bundle of information back as well. Part of this bundle is a collection of cookies. But its NOT the same collection as was sent by the client in the first place. They are two different collections.

While the Request.Cookies collection will contain all of the data that is relevant to you from the client (and is mostly read only data), the Response.Cookies collection is empty until you put data in it. This is so that only cookie data that has changed will get sent back to the client (no point in sending back data the client already has).

Also, you cant just check Response.Cookies to see if a cookie is blank or not, since a cookie will get created if one doesnt exist already when you try and access the cookie.

I was reading my cookie collections around the wrong way :)

The big problem I can see now is that I need some mechanism to show when I have updated my Response.Cookies data as if I check it I risk sending an empty cookie back that will overwrite the proper one. I suspect a write thru cache will do it, so I dont accidentily create empty cookies when I read the cache.


Edit: Update - also have to remember that when you write to Response.Cookie it has to be done before the <html> tag, or the cookie wont get written.

Edited by - Tiggerz on 30 November 2004 22:01:30
Go to Top of Page

dayve
Forum Moderator

USA
5820 Posts

Posted - 30 November 2004 :  22:18:45  Show Profile  Visit dayve's Homepage
quote:
Originally posted by Tiggerz

The big problem I can see now is that I need some mechanism to show when I have updated my Response.Cookies data as if I check it I risk sending an empty cookie back that will overwrite the proper one. I suspect a write thru cache will do it, so I dont accidentily create empty cookies when I read the cache.


Just create another cookie with the time the other cookie was written. Example:
Response.Cookie("myCookie")("field") = "hello"
Response.Cookie("myCookie")("ckDateTime") = Now()

Then when you access the cookie, you can do a date comparison from the value of the cookie with the current time, or something similar to that kind of scenario.
quote:
Originally posted by Tiggerz

Edit: Update - also have to remember that when you write to Response.Cookie it has to be done before the <html> tag, or the cookie wont get written.


That's not a correct statement. It can basically be anywhere on the page. Keep in mind that ASP is done server side, so the majority of the time, it doesn't matter where you put it. It is only when you wish to return a value that it matters where you place it on the page, but it surely does not have to be before the <html> tag.

Go to Top of Page

Tiggerz
Starting Member

45 Posts

Posted - 01 December 2004 :  00:29:48  Show Profile
You may wanna test that last bit. I tried everything to get it to work and it only did when I placed the update before the html tag. Otherwise it worked - in the page, but didnt commit the cookie to the client, so next time I loaded the page, I got the old data back.
Go to Top of Page

pdrg
Support Moderator

United Kingdom
2897 Posts

Posted - 01 December 2004 :  04:38:52  Show Profile  Send pdrg a Yahoo! Message
I think the response cookies come back in the response header, which must be sent before the page is rendered, so that would fall into line with 'before the <html> tag'. I guess ti's a bit like the response buffering and using response.redirect before any 'page' stuff is sent.
Go to Top of Page

dayve
Forum Moderator

USA
5820 Posts

Posted - 01 December 2004 :  10:58:09  Show Profile  Visit dayve's Homepage
It does not need to be done before the <html> tag... here is a really, really, really simple example:

<html>
  <title>Cookies</title>
 <body>
<% 
 name = "Dayve"
  Response.Write name & "<br />"
  Response.Cookies("name") = "Not Dayve"
 name = Request.Cookies("name")
  Response.Write name & "<br />"
 </body>
</html>
%>

A recommendation. Test the stuff before debunking what someone says


Edited by - dayve on 01 December 2004 10:59:16
Go to Top of Page

pdrg
Support Moderator

United Kingdom
2897 Posts

Posted - 01 December 2004 :  11:06:53  Show Profile  Send pdrg a Yahoo! Message
so I'm wrong...man I'm a bit rusty now and don't even have test kit available, so was just trying to work out the underlying whatever. Maybe this question is still to do with the buffer state though - I can't remember the correct terms, but if the header has already been sent (ie the page is unbuffered) then how could you possibly set the cookie? If the page is buffered, I guess I could see how this would work fine.

Or am I missing something - I would love to understand what's really going off under the covers here!
Go to Top of Page

dayve
Forum Moderator

USA
5820 Posts

Posted - 01 December 2004 :  11:42:03  Show Profile  Visit dayve's Homepage
Response.Buffer = False will definitely cause problems, causing the infamous error:

The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.

This is when your setting of cookies must be done before the <html> tag.

Go to Top of Page

Tiggerz
Starting Member

45 Posts

Posted - 01 December 2004 :  15:08:49  Show Profile
http://www.w3schools.com/asp/asp_cookies.asp

At the very top its says Response.Cookies must appear before the html tag. Also, as I said before, it does not seem to write the cookie to the client if its after the html tag. ALthough you can set it and read it to hearts content, it wont be saved if its after the html tag.

Out of interest I spent a couple of hours debugging why my cookies were not being committed. Started working as soon as I moved the response before the html tag. So on my setup IIS5/XP SP2 it works as stated in w3schools.

Edited by - Tiggerz on 01 December 2004 15:10:24
Go to Top of Page
Page: of 3 Previous Topic Topic Next Topic  
Next Page
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.44 seconds. Powered By: Snitz Forums 2000 Version 3.4.07