Search Tools Links Login

write cookies


When a page is loaded, the browser uses a piece of memory to store the pages information. When the user exists the browser, this information can be written to a flat file on the client machine or on the server. This flat file is called a 'cookie'. Generally cookies are harmless, they do not contain any executable code. By retaining information, the cookie can assist the user to have a better web experience by retaining settings and information from the last time that the user was one the page.

Original Author: Bhushan-

Code

When a page is loaded, the browser uses a piece of memory to store the pages information. When the user exists the browser, this information can be written to a flat file on the client machine or on the server. This flat file is called a 'cookie'. Generally cookies are harmless, they do not contain any executable code. By retaining information, the cookie can assist the user to have a better web experience by retaining settings and information from the last time that the user was one the page.
Here is a simple example which sets a cookie and stores a value called last visit. try it out
<%@ LANGUAGE="VBScript" %>
<% Response.Buffer = true %>


Cookies in ASP













Cookies in ASP


 

When this script runs, it first checks for a cookie named 'lastvisit'. If
found, it displays a message showing the date and time stored in that cookie.
Then it takes the current date and time and saves them in this cookie.


If you reload the page or visit it again later, it will display a timestamp,
showing you when you were last here (try hitting your RELOAD button if you
don't see it on the next line).


<% 'Get date and time of last visit from cookie.
last = Request.Cookies("lastvisit")
if last <> "" then
Response.Write("You last visited this page on " & last & ".")
end if %>


One important thing to note in the source is the line at the beginning of the
page.



Response.Buffer = true


This tells the server to run all the script code in the page before sending any
data to the browser. By default, script output is not buffered, and the server
may start transmitting the page to the browser even as the code is still being
executed.


Here we want to set cookie information, which is stored as part of the response
header. Everytime a file is transmitted over the web, a short header is
attached to the front of it. When a web server sends a page to a browser this
header contains, among other things, data which tells the browser what kind of
file it is (image, HTML, plain text, etc.). It can also contain cookie data for
the browser to store.


Since the code that actually sets the cookie value is farther down in the page,
the output needs to be buffered so the cookie can be set before the response
header is sent. Therefore, it is often a good idea to set buffering on anytime
you use cookies within your scripts.


Also note these two lines.



last = Request.Cookies("lastvisit")
...
Response.Cookies("lastvisit") = Now


The Request object contains data found in the request header sent by the
browser. This will include any cookie data, form input and browser info. The
Response object represents the data that will from the web server back to the
browser, including the header and page contents.


Source








Home

<% 'Save current date and time in cookie
Response.Cookies("lastvisit") = Now
Response.Cookies("lastvisit").Expires = DateAdd("d", 30, Date) %>



About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 76 times

Categories

ASP/ HTML

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.