Author |
Topic  |
Tiggerz
Starting Member
45 Posts |
Posted - 29 November 2004 : 22:36:10
|
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
|
Dim cookText cookText = Request.Cookies("myCookie")( "field" ) Response.Write( cookText )
|
|
 |
|
Tiggerz
Starting Member
45 Posts |
Posted - 30 November 2004 : 03:46:44
|
Whats the difference between the response and request. I figure two different collections, but why ?
|
 |
|
muzishun
Senior Member
   
United States
1079 Posts |
Posted - 30 November 2004 : 04:55:11
|
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) |
 |
|
pdrg
Support Moderator
    
United Kingdom
2897 Posts |
Posted - 30 November 2004 : 10:04:43
|
it's an HTTP thing - the browser requests a page from the server. the server responds :) Try the RFC's for more details :) |
 |
|
dayve
Forum Moderator
    
USA
5820 Posts |
|
Tiggerz
Starting Member
45 Posts |
Posted - 30 November 2004 : 17:56:50
|
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.
|
 |
|
dayve
Forum Moderator
    
USA
5820 Posts |
Posted - 30 November 2004 : 18:19:51
|
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 |
 |
|
Tiggerz
Starting Member
45 Posts |
Posted - 30 November 2004 : 20:41:35
|
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 |
 |
|
dayve
Forum Moderator
    
USA
5820 Posts |
Posted - 30 November 2004 : 22:18:45
|
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. |
|
 |
|
Tiggerz
Starting Member
45 Posts |
Posted - 01 December 2004 : 00:29:48
|
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. |
 |
|
pdrg
Support Moderator
    
United Kingdom
2897 Posts |
Posted - 01 December 2004 : 04:38:52
|
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. |
 |
|
dayve
Forum Moderator
    
USA
5820 Posts |
Posted - 01 December 2004 : 10:58:09
|
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 |
 |
|
pdrg
Support Moderator
    
United Kingdom
2897 Posts |
Posted - 01 December 2004 : 11:06:53
|
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! |
 |
|
dayve
Forum Moderator
    
USA
5820 Posts |
Posted - 01 December 2004 : 11:42:03
|
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. |
|
 |
|
Tiggerz
Starting Member
45 Posts |
Posted - 01 December 2004 : 15:08:49
|
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 |
 |
|
Topic  |
|