|
|
|
|
More of the Request and Response Object
Cookies play a very important role in managing states in stateless environment. Since HTTP is stateless protocol, there is no persistence or connection state being maintained. The official name for cookies is HTTP state management mechanism We have already seen one way of managing states-the session object. Here the cookies were transparently managed by the server. We will now learn how to read and write data to cookies through programming code.
Cookies are server specific. The cookie's attributes store the domain and path from where the cookie is written.
These serve to uniquely identify cookies and prevent conflict if two web sites
use cookies with the name or if multiple application on same server use cookies with same names.
Hence if www.mydomain.com/asp/
is one application and www.mydomain.com/asp/tutorial/
is another application, both of which track the user using a cookie named
dtlastvisit, the server would not be confused when trying to read them, since the path is stored along with the data. Response.Cookies("CookieName")="Value" The following syntax sets cookies with keys: Response.Cookies("CookieName")(Keyname1)="Value" For example, the following sets a cookie named lastvisitedwith the current date: <% Response.Cookies(LastVisited")= Date() %> The following code stores the user's login and password on his system: <% The request object can read this data from its cookies collection. the syntax is similar to that of the response object: The following code reads the lastvisited and the user cookies written to the client and outputs it to the HTML stream: <%
The attributes of a cookie include Domain, Path ,Expires, HasKeys and Secure. The attributes are accessed and set in the following general form (the key is optional): [Request | Response]. cookies("cookieName")("Keyname").Attribute As discussed earlier, the Domain and path provide the mechanism to uniquely identify a cookie, as they store the domain and the path from where the cookie was written. The expires attribute sets a date when the cookie expires. the server ignores its values after this date. The HasKeys attribute returns true if the cookie has keys. the secure attribute tells the server whether the cookie is used in a secure transaction. This is the attributes for the Cookie are set: <%
A collection of the Request object, the Server Variables collection holds information about the server and the client. For convenience, it can be grouped into three parts; the HTTP Request ,the Server Environment and all other data items. The HTTP Request group holds information about the HTTP transaction. It holds variables like Request_Method, Content_Length, Content_Type, HTTPS, HTTPS_KeySize, etc. The server Environment group holds information about the server, which includes variables like Server_Name Server_Software, Server_Port, Server_Protocol, etc. The rest of the Server Variables collection holds information about the various collections and variables of the form HTTP_Header_Name>.For example, HTTP_Cookie holds information about the Cookies collection. Other variables include HTTP_Referrer, HTTP_User_Agent, etc. |