A cookie is a small text file that is stored on clientmachine that is embeded by the server. Each time the same computer requests a page with a browser, it will send the cookie too.
You can both create and retrieve cookie values in ASP.
To store information specific to a visitor of your website,ASP Cookies are used.
For an extended amount of time this cookie is stored to the user's computer.
You can set the expiration date of the cookie for some day in the future it will remain their until that day unless manually deleted by the user
How to Create a Cookie
To create cookies,the "Response.Cookies" command is used .
BEFORE the <html> tag,the Response.Cookies command must appear
We create an welcome cookie in the example below
In the example below, we will create a cookie named "CountVisitors" and assign the value numvisits to it
ASP Code:
<%
dim numvisits
response.cookies("CountVisitors").Expires=date+365
numvisits=request.cookies("CountVisitors")
if numvisits="" then
response.cookies("CountVisitors")=1
response.write("Welcome! This is the first time you are visiting this Web page.")
else
response.cookies("CountVisitors")=numvisits+1
response.write("You have visited this ")
response.write("Web page " & numvisits)
if numvisits=1 then
response.write " time before!"
else
response.write " times before!"
end if
end if
%>
<html>
<body>
</body>
</html>
O/P:
You have visited this page first time.
How to Retrieve a Cookie Value?
To retrieve a cookie value,the "Request.Cookies" command is used.
In the example below, we retrieve the value of the cookie named "goodname" and display it on a page
Assume that your server has sent all the cookies above to a user.
Now we want to read all the cookies sent to a user. The example below shows how to do it (note that the code below checks if a cookie has Keys with the HasKeys property):>li
<html>
<body><%
dim x,yfor each x in Request.Cookies
response.write("<p>")
if Request.Cookies(x).HasKeys then
for each y in Request.Cookies(x)
response.write(x & ":" & y & "=" & Request.Cookies(x)(y))
response.write("<br />")
next
else
Response.Write(x & "=" & Request.Cookies(x) & "<br />")
end if
response.write "</p>"
next
%></body>
</html>
You will have to use other methods to pass information from one page to another in your application if your application deals with browsers that do not support cookies,
There are two methods of doing this:
1.Add parameters to a URL
2. Use a form
1.Add parameters to a URL
You can add parameters to a URL:
<a href="welcome.asp?fname=John&lname=Smith">
Go to Welcome Page</a>
And retrieve the values in the "welcome.asp" file like this:
<%
fname=Request.querystring("fname")
lname=Request.querystring("lname")
response.write("<p>Hello " & fname & " " & lname & "!</p>")
response.write("<p>Welcome to my Web site!</p>")
%>
2.Use a form
You can use a form. When the user clicks on the Submit button,the form passes the user input to "welcome.asp":
<form method="post" action="welcome.asp">
First Name: <input type="text" name="fname" value="">
Last Name: <input type="text" name="lname" value="">
<input type="submit" value="Submit">
</form>
And retrieve the values in the "welcome.asp" file like this:
<%
fname=Request.form("fname")
lname=Request.form("lname")
response.write("<p>Hello " & fname & " " & lname & "!</p>")
response.write("<p>Welcome to my Web site!</p>")
%>
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
asp cookies 2005, active server pages, asp cookies response.write,
cookie values