Latest web development tutorials

ASP Cookies

cookie used to identify the user.


Examples

Try - Example

Welcome cookie
This example demonstrates how to create a Welcome cookie.


What Cookie that?

cookie used to identify the user. A cookie is a server on the user's computer to stay in a small file. Whenever the same computer via the browser requests a page, this computer will send a cookie. By ASP, you can create and retrieve cookie values.


How to Create a Cookie?

"Response.Cookies" command is used to create a cookie.

Note: Response.Cookies command must appear before the <html> tag.

In the example below, we will create a cookie named "firstname" and will assign it to "Alex":

<%
Response.Cookies("firstname")="Alex"
%>

Assigned to the cookie attribute is also possible, for example, set the cookie expiration time:

<%
Response.Cookies("firstname")="Alex"
Response.Cookies("firstname").Expires=#May 10,2012#
%>


How to retrieve the value of the Cookie?

"Request.Cookies" command is used to retrieve a cookie value.

In the following example, we retrieve the value named "firstname" of the cookie, and the value displayed on the page:

<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>

Output: Firstname = Alex


Cookie with key

If a cookie contains a collection of multiple values, we say that the cookie with keys (Keys).

In the following example, we will create a cookie collection named "user" of. "User" cookie contains the user with key information:

<%
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
%>


Read all of the Cookie

Please read the following code:

<%
Response.Cookies("firstname")="Alex"
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
%>

Suppose your server will above all cookie passed to a user.

Now, we need to read them all to pass a user cookie. The following example shows you how to do this (note that the code below checks if a cookie by HasKeys property with a key):

<!DOCTYPE html>
<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>

Output:

firstname = Alex

user: firstname = John
user: lastname = Smith
user: country = Norway
user: age = 25


If your browser does not support Cookie how to do?

If your application needs to deal with do not support the browser cookie, you have to use other methods to pass information in your application between pages. There are two ways:

1. Add parameters to the URL

You can add parameters to the URL:

<a href="welcome.asp?fname=John&lname=Smith">Go to Welcome Page</a>

Then retrieve these values ​​in the "welcome.asp" file, as follows:

<%
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 Form

You can use the form. When the user clicks the Submit button, the user input form will pass "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>

Then retrieve these values ​​in the "welcome.asp" file, as follows:

<%
fname=Request.form("fname")
lname=Request.form("lname")
response.write("<p>Hello " & fname & " " & lname & "!</p>")
response.write("<p>Welcome to my Web site!</p>")
%>