A cookie is often used to identify a user.
A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With ASP, you can both create and retrieve cookie values.
The "Response.Cookies" command is used to create cookies.
Note: The Response.Cookies command must appear BEFORE the <html> tag.
In the example below, we will create a cookie named "firstname" and assign the value "Alex" to it:<%
It is also possible to assign properties to a cookie, like setting a date when the cookie should expire:<%
The "Request.Cookies" command is used to retrieve a cookie value.
In the example below, we retrieve the value of the cookie named "firstname" and display it on a page:<%
If a cookie contains a collection of multiple values, we say that the cookie has Keys.
In the example below, we will create a cookie collection named "user". The "user" cookie has Keys that contains information about a user:<%
Look at the following code:<%
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):<!DOCTYPE html>
If your application deals with browsers that do not support cookies, you will have to use other methods to pass information from one page to another in your application. There are two ways of doing this:
You can add parameters to a URL:<a href="welcome.html?fname=John&lname=Smith">Go to Welcome Page</a>
And retrieve the values in the "welcome.html" file like this:<%
You can use a form. The form passes the user input to "welcome.html" when the user clicks on the Submit button:<form method="post" action="welcome.html">
Retrieve the values in the "welcome.html" file like this:<%