We will make a page that remembers the visitor's name, and in this small case you will learn how to use the values of the Request object's cookies, form, and ServerVariables collection, and learn how to use the Response object to send cookies.
First take a look at the program code:
<%@LANGUAGE="VBSCRIPT"CODEPAGE="65001"%><!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><title>Remembering visitors ' names with cookies</title></Head><Body><%DimSusernamesusername= Trim(Request.Cookies ("name"))'To determine if name is empty and not NULL to output the value of nameIfsUserName= "" Then 'determine if post has just submitted a form, then get form content output Cookies If UCase(Trim(Request.ServerVariables ("Request_method"))) = "POST" ThensUserName= Trim(Request.Form ("name")) Response.Cookies ("name") =sUserName Response.Cookies ("name"). Expires= DATEADD("D", 1, Now) 'cookies expire after one dayResponse.Write ("I have already remembered your name! ") Else 'Otherwise, the form is displayed so that the user submits the form%> <formMethod= "POST"Action="">please tell me your name:<inputname= "Name"type= "text"/> <inputtype= "Submit"value= "Submit" /> </form><% End If ElseResponse.Write ("Hello," &sUserName)End If%></Body></HTML>
First run, unable to get Cookies information, display the form, let the user submit, such as:
Submit a form, or post to the current ASP page, because you still can't get cookies, so display the page that submitted the form successfully, such as:
Refresh the current page requestcookies.asp again, as you can get a cookie to display the visitor's name directly.
We explain in detail the part that lets the user enter the name and save, first get the value of the ServerVariables variable Request_method, which identifies the current page request method, if it is the post method, it is now thinking this page to submit a form, This is the time to get the value of the form and use the Response.Cookies collection to output cookies to the client, otherwise display the HTML code for the user to fill in the name.
Small Knowledge
The Trim function removes spaces on either side of the string, theLTrim function deletes the left space of the string, and theRTrim function removes the right space.
The UCase function converts the specified string to uppercase, and theLCase function converts the specified string to lowercase.
Sample code Download
Requestcookies.rar
Introduction to ASP (eight)-request object small case