dreamweaver| programming
Example four, combining DW MX server behavior to verify the legality of forms on the server side
Although you can use the DW mx "behavior" of the check form to generate JS code to check the user submitted form data is legitimate, but this is done on the client, our users thousand odd, if the use of a lower version of the browser may cause client inspection failed, the illegal information into the database. This is not allowed. Below we use the above learning request object and judgment statement to add to DW MX "Server Behavior" code, implement server-side quarantine form, to eliminate illegal form information.
Create a new ASP page, use Access to make a datasheet, design two fields: "Uers", "PWS", and set the fields to be required. Connect the database with a custom string. Inserts a form, three text fields, a button in the page, the form method is: "POST", the type is "submit form", and the three text field names correspond as follows:
User name: Uers
Password: PWS
Duplicate Password: pws2
Now, require that these three items are required, and "password" must be equal to "duplicate password", otherwise tell the operator, input error, please return!. The following steps are in action:
Example three (3)
1, insert "Insert record" server behavior, press "F12" key preview page, can insert the record normally.
2, find the following sentence code:
<% ' * * * Insert record:set variables If (CStr ("mm_insert") = "Form1") Then MM_editConnection = mm_new_string MM_editTable = "Use" MM_editRedirectUrl = "" MM_fieldsStr = "Uers|value|pws|value" MM_columnsStr = "cuse|", none, ' |pws| ', none, ' " ' Create the MM_fields and mm_columns arrays MM_fields = Split (MM_fieldsStr, "|") MM_columns = Split (mm_columnsstr, "|") .......... Insert Record code .... %> |
In the above sentence "If" (CStr ("mm_insert") = "Form1") Then, enter the following code:
If request. Form ("uers") = "" Then Response.Write "Sorry!" Please enter user name! Please <a href= ' untitled-5.asp ' > return! </a> " Response.End ElseIf Request.Form ("PWS") = "" Then Response.Write "Sorry!" Please enter the password! Please <a href= ' untitled-5.asp ' > return! </a> " Response.End ElseIf Request.Form ("pws2") = "" Then Response.Write "Sorry!" Please enter a duplicate password! Please <a href= ' untitled-5.asp ' > return! </a> " Response.End ElseIf Request.Form ("PWS") <>request.form ("Pws2") Then Response.Write "Sorry!" Two input passwords do not match! Please <a href= ' untitled-5.asp ' > return! </a> " Response.End End If Shaped like: If (CStr ("mm_insert") = "Form1") Then If request. Form ("uers") = "" Then Response.Write "Sorry! Please enter user name! Please <a href= ' untitled-5.asp ' > return! </a> Response.End ElseIf request.form ("PWS") = "then Response.Write" I'm sorry! Please enter the password! Please <a href= ' untitled-5.asp ' > return! </a> Response.End ElseIf request.form ("pws2") = "then Response.Write" I'm sorry! Please enter a duplicate password! Please <a href= ' untitled-5.asp ' > return! </a> Response.End ElseIf request.form ("PWS") <>request.form ("Pws2") then Response.Write "I'm sorry!" Two input passwords do not match! Please <a href= ' untitled-5.asp ' > return! </a> Response.End End If "handwriting code close MM_editConnection = mm_new_string MM_editTable = "Use" MM_editRedirectUrl = "" MM_fieldsStr = "Uers|value|pws|value" MM_columnsStr = "Cuse|" , none, ' |pws| ', none, ' |
In this case, in addition to the request object, we used the write method output hint for the response object, and the end method to stop the script execution, blocking the Response.End code continues to execute so that the data is not inserted into the code.
Well, the introduction to the judgment statement is here, you have to grasp this content, in the design forum, it is useful.
Second, Response object--output data to the client
The example described earlier is useful for the write method of the response object to output data and end script execution to the client. This section will describe it in more detail.
The response object works against the request object, and the response object is processed from the data received by the request object from the client, and then the "finished product" is exported to the client browser.
Response Object Syntax Format:
response[(SET) | properties | methods]
First try the magical charm of the response object.
1, the use of Response.Write Method--to direct client browser output data
Instance one, using the response object to output data information to the client
Create a new ASP page and enter the following statement within the <body></body> label:
<%response.write ("Welcome to learn Response object examples!") ")%>
Complete code:
<% @LANGUAGE = "VBSCRIPT" codepage= "936"%> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> <title> Untitled Document </title> <body> <%response.write ("Welcome to learn Response object examples!") ")%> </body>
|
Press the "F12" button to try, is not the text displayed?
The Response.writ method can also output the result or function of a string, and if it is a variable, define and assign the value first. Response.Write can output HTML standard format code.
Example two, using Response.Write to output the current system time and HTML hyperlinks
Enter the following code between the <body> and </body> tabs of the new file:
<p> <%response.write (now ())%> </p> <p> <% Response.Write ("<a href= ' index.asp ' > Open homepage </a>")%> |
Here's <a href= ' index.asp ' > Open homepage </a> is standard HTML format, but ' index.asp ' uses single quotes to avoid confusion with ASP script conflicts.
Try the effect by "F12".
Note: Commonly used time functions:
Now () gets the current system date and time, which can be written by the ASP output: <%=now ()%>
Year (now ()) to get years, ASP output: <%=year (now ())%>
Month (now ()) Gets the current month, ASP output: <%=month (Today ())%>
Day (now ()) Gets the number of days, ASP output: <%=day (now ())%>
Minute (now ()) Gets the number of minutes, ASP output: <%=minute (now ())%>
Second (now ()) Gets the number of seconds, ASP output: <%=second (now ())%>
Date () Gets the current system day, in the format: 2004-2-28
Time () Gets the current system date in the format: 22:24:59
When you output string information in the Response.Write method, the "()" collection must have a pair of "" "double quotes in the English way. If it is a function and a numeric class of data is not necessary, such as:
Text string output:<% Response.Write ("Output text information")%>
Function output:<% Response.Write (Now ())%>
Digital output:<% Response.Write (1142568147)%>
You can also use "&" to correctly differentiate between numbers or functions and text mixed output:
<% yy= "The value of the variable here"is assigned to the variable "yy" if it is text plus " " "
Response.Write (Now () & "Here is the output text" & "" &yy)%>
2, Response.Redirect Method-link to the new Web site
If you want users to be able to link to another URL in the browser, theResponse.Redirect method can do this. Perhaps some friends will think of using JS code can also achieve this function Ah! However, we are executing on the server side and not affected by the client browser version.
Example three, "Friendship link" page
Create a new ASP page, insert the form, List/menu, push button, the form method is "get", "List/menu" name is "url", "Target" is "_blank", and the action chooses itself. The following figure:
The values and labels for the list/menu are as follows:
Enter the following code above the
<% Select Case Request. QueryString ("url") Case "1" Response. Redirect ("http://www.163.com") Case "2" Response. Redirect ("http://www.sina.com") Case "3" Response. Redirect ("http://www.sohu.com") Case "4" Response. Redirect ("http://www.6to23.com") End Select %> |
Complete code below:
<% @LANGUAGE = "VBSCRIPT" codepage= "936"%> <% Select Case Request. QueryString ("url") Case "1" Response. Redirect ("http://www.163.com") Case "2" Response. Redirect ("http://www.sina.com") Case "3" Response. Redirect ("http://www.sohu.com") Case "4" Response. Redirect ("http://www.6to23.com") End Select %> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> <title> Untitled Document </title> <body> <form action= "untitled-6.asp" method= "Get" Name= "Form1" target= "_blank" > <select name= "url" id= "url" > <option value= "1" > NetEase </option> <option value= "2" > Sina </option> <option value= "3" > Sohu </option> <option value= "4" > China Student Network </option> </select> <input type= "Submit" name= "Submission" value= "submitted" > </form> </body>
|
Press "F12" button, choose the website you want to go to!
3, so that the Response.Write method to achieve cyclic output
This involves a VBScript scripting language, which is simple, but useful, and is used when we want to do a recordset paging.
For...next LOOP Statement structure:
For variable = starting value to end value Step value
Circulation body
Next
Here's a little example:
Instance five, the number of circular output characters
<% @LANGUAGE = "VBSCRIPT" codepage= "936"%> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> <title> Untitled Document </title>
<body> <% for Y=1 to 50%> <%response. Write ("word")%> <%next%> </body>
|
Press the "F12" key to preview, and the results are as follows:
Visible, it repeats a word 50 times after the output, if no step value is set, the default value (1) will be processed. To set the step value:
<% for Y=1 to-step 2%>
<%response.write (y)%>
<%next%>
The results of the implementation are as follows:
Visible, the output is "2" for stepping.
You can also change:
<%response. Write ("word")%>
For:
<%response.write (y)%>
Look at the results of the implementation below:
It takes the "1" loop starting at 1 as the step output until "50" begins.
We will only learn this circular statement first. There are other do...loop, do-while...loop, do-Until...loop, while...wend-loop structures here, and maybe we won't be able to touch these things for the time being.
4, Response object properties
The response object has the following properties:
- Buffer server buffers.
- CharSet attaches the character set name to the back of the Content-type header in the response object.
- ContentType the HTTP content type that specifies the server response.
- expires specifies to buffer the storage page expiration time on the browser.
- ExpiresAbsolute specifies the exact expiration time on the browser to buffer the storage page.
Response Object Method:
- Write directly outputs information to the client.
- redirect turn to address.
- End stops program execution.
- Flush is used to send output immediately in the server buffer, the response.buffer=true must be set, or the error will be.
- Clear clears all HTML output in the server buffer.
Response Collection:
- Cookies are used to set the value of a cookie.
Response.Cookies Collection Syntax Format:
Response.Cookies (variable name) [(Key) | Property]= equal to write information
"Variable name" specifies the name of the cookie so that the page can read the data in different cookies.
Iii. Session Object-server-specific user sessions
The session object is called a "stage variable" in the Help documentation for the DW MX, and when we insert the "Log on user" and "Restrict access to page" Server behavior on the page, it uses the sessions object to identify whether the user is logged in. When the session is created, the server traces the page and allocates memory to store session information. Session sessions information is only visible to the user who created it.
Instance one, using the Request.Form method to store information in session sessions
Create a new ASP page, enter this code above the <HTML> tag:
<%session ("MM") =request. Form ("Y")%>
Enter the following code between <body> and </body>:
<form action= "untitled-6.asp" method= "Post" Name= "Form1" > <select name= "y" id= "y" > <option value= "NetEase" > NetEase </option> <option value= "Sina" > Sina </option> <option value= "Sohu" > Sohu </option> <option value= "China Student Network" > China Student Network </option> </select> <input type= "Submit" name= "Submission" value= "submitted" > </form> <p><%= session ("MM")%></p> |
Where the<%= session ("MM")%> is displayed on the page for the value of reading a session variable.
Press the "F12" key to preview the results of the execution.
If you use request. QueryString method Number data is stored in the Session object, the <%session ("MM") =request. Form ("y")%>
To
<%session ("MM") =request. QueryString ("Y")%>
Can.
1, Session.adandon method
The session object has only one method, the Abandon method, which is to delete all data stored in the session object and to interpret the resources. If the Adandon method is not explicitly invoked, when the session timeout (the IIS server default timeout of 900 seconds, which is determined by the space server vendor), the server deletes the sessions to free resources.
Syntax below:
<%session.adandon%>
Analysis of "Logoff user" code for DW mx
<% ' * * * Logout the current user. Mm_logoutredirectpage = "Index.asp" Session.Contents.Remove ("Mm_username") Session.Contents.Remove ("Mm_userauthorization") If (Mm_logoutredirectpage <> "") Then Response.Redirect (mm_logoutredirectpage) %> |
The Session.Contents.Remove property is used in this code to specify a session variable to delete, if "contents.remove" is not specified and "Adandon" is used. , it will delete all sessions created by the page, but sometimes we just need to delete one or more of the sessions and keep the other session state.
To delete multiple sessions:
Session.Contents.Remove ("Mm_username")
Session.Contents.Remove ("Mm_userauthorization")
...
The following is the code generated by the "Restrict access to pages" Server behavior of DW MX:
<% ' * * * Restrict access to page:grant or deny access to this Page Mm_authorizedusers= "" Mm_authfailedurl= "Yesno.asp" Mm_grantaccess=false If session ("Mm_username") <> "Then" If (True Or CStr (session ("Mm_userauthorization")) = "") Or _ (InStr (1,mm_authorizedusers,session ("Mm_userauthorization")) >=1) Then Mm_grantaccess = True End If End If If not mm_grantaccess Then Mm_qschar = "?" If (InStr (1,mm_authfailedurl, "?") >= 1 Then mm_qschar = "&" Mm_referrer = Request.ServerVariables ("URL") if (Len (Request.QueryString ()) > 0) Then mm_referrer = Mm_referrer & "?" & Request.QueryString () Mm_authfailedurl = Mm_authfailedurl & Mm_qschar & "accessdenied=" & Server.URLEncode (Mm_referrer) Response.Redirect (Mm_authfailedurl) End If %> |
The phrase "if Session" ("Mm_username") <> "Then " is used to determine whether the "mm_username" conversation variable has a value, otherwise the Response.Redirect (Mm_authfailedurl)"Turn to error page."
In fact, we can write a simpler one:
<% if session ("mm_uesrname") = "" Then Response. Redirect ("error page. asp") Response.End End If %> |
This code determines whether the "mm_uesrname"session variable is empty, and, if so, with response. Redirect (the error page. asp) method takes the user to the error page. aspand stops all script processing with the Response.End method, so this code is best placed on the head of the page.
can also put
Response. Redirect ("index.asp")
To
Response.Write ("Sorry!") Please log in! ")
This is done directly on the same page to complete the error prompts, do not have to page around so that the site file management.
Well, if you can skillfully use the above three ASP built-in basic objects, you can change the DW MX server behavior to achieve our goal. There are some other objects here, because the DW MX server behavior can generate this code for us, you do not have to start from the bottom of the ASP to learn the same can be compiled out of the general program.
Previous page