How to Send and Receive Cookies in ASP?
Hi friends,
I have recently started with the Active Server Pages (ASP). I know the purpose of the cookies. I have tried them to send and receive in Java. But I don't know how to Send and Receive Cookies in ASP? I have tried many different methods, but was not successful. Since, I am new to ASP, I request to all that please explain me considering the newbie. Some sample of coding would be really grateful. Hoping that someone would help me soon.!!:notworthy
Re: How to Send and Receive Cookies in ASP?
For identifying an user, a cookie is used. I think that you should know more about the cookie. Once, if you know the actual functions of cookie, then it might help you while designing the code. A small file that the server embeds on the user's computer can be called as cookie. Whenever, the same computer requests a page with a browser, it will have to send a cookie. You can create as well as retrieve cookie values, by using an ASP. The function of ASP sessions are also very much similar to cookie. Similarly like ASP Sessions, ASP Cookies are used to store information specific to a visitor of your website.
Re: How to Create Cookies in ASP?
You should know how to create cookies in ASP.!! Since, you said that you are new, I think that creating a cookie is important for you. For creating a cookie the "Response.Cookies" command is used. You must keep in mind that you will have to write the Response.Cookies command before the <html> tag. You can see the following example :
Code:
<%
Response.Cookies("carname")="HondaCity"
%>
In the above example, I have created a cookie named "carname" and assigned the value "HondaCity" to it.
You can also set the date when you want the cookie to expire. I have explained the same thing in following code :
Code:
<%
Response.Cookies("carname")="HondaCity"
Response.Cookies("carname").Expires=#March 16,2010#
%>
Re: How to Send and Receive Cookies in ASP?
I am assuming that you are clear with the cookies concept. You can use the response.cookies collection object for sending the cookies to the browser. I have provided you with the syntax and comments, so that it would be easy to understand you. The following is used to send a cookie :
Quote:
set c = response.Cookies
c.Item(place) = value 'Adding a value with a place.
c(place) = value 'Same as above - short-hand form
c.Add value, place 'Same as above
c.Remove(place) 'Removing a place and its value
Once you send the cookies, then the receiving cookies from the browser can be done using the request.Cookies collection object. Check the following for the same :
Quote:
set c = request.Cookies
value = c.Item(place) 'Returning the value of the specified item.
value = c(place) 'Same as above - short-hand form.
for each n in c 'Iterating by place
v = c(n)
next
Re: How to Send and Receive Cookies in ASP?
If you want to read all the cookies, then you can check the following example :
Suppose the following are sent to the user by the server :
Code:
<%
Response.Cookies("Continent")="Asia"
Response.Cookies("user")("subcontinent")="China"
Response.Cookies("user")("country")="China"
Response.Cookies("user")("years")="25"
%>
If you want to send all information to the user, you can do that by following way :
HTML Code:
<html>
<body>
<%
dim x,y
for 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>
Re: How to Send and Receive Cookies in ASP?
I wold to tell you that, you can also add the parameters to a URL in ASP. You can use the below code for adding the parameters to URL :
Code:
<a href="addpara.asp?fname=Miyu&lname=Juin">Go to Add Parameters Page</a>
And retrieve the values in the "addpara.asp" file like the following code :
Code:
<%
fname=Request.querystring("fname")
lname=Request.querystring("lname")
response.write("<p>Welcome " & fname & " " & lname & "!</p>")
response.write("<p>This is my Web site!</p>")
%>