A cookie is often used to identify a user.
How to create a Welcome cookie.
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):
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:
And retrieve the values in the "welcome.asp" file like this:
You can use a form. The form passes the user input to "welcome.asp" when the user clicks on the Submit button:
Retrieve the values in the "welcome.asp" file like this:
Your message has been sent to W3Schools.