190 likes | 294 Views
16 – Passing Data between pages: Sessions, Query Strings, & Self Posting. Session Aims & Objectives. Aims To introduce the fundamental ideas involved in passing data between pages Objectives, by end of this week’s sessions, you should be able to: pass data between pages , using:
E N D
16 – Passing Data between pages: Sessions, Query Strings, & Self Posting
Session Aims & Objectives • Aims • To introduce the fundamental ideas involved in passing data between pages • Objectives,by end of this week’s sessions, you should be able to: • pass data between pages, using: • Self Posting • Query Strings • Session Variables
Example: Logon • Restrict access tohome page
Example: Logon - code (v2) LoginFail.aspx Home.htm <html> <head> <title></title> <% If Request.Form("txtUserName") = "mark" And Request.Form("txtPassWord") = "soft131" Then Response.Redirect("home.htm") End If %> </head> <body> <p>Sorry, those login details were incorrect. <p>Please try <a href=Logon.htm>again</a> </body> </html> <html> <head> <title>My Home page</title> </head> <body> <p>Welcome to my home page.<br> <img src="YouAreHere.jpg" WIDTH="450" HEIGHT="259"> </body> </html> Logon.htm <html> <head> <title></title> </head> <body> <p>Please logon: <form action=LoginFail.aspx method=post> <input name=txtUserName type=text> <input name=txtPassWord type=text> <input name=btnLogon type=submit value=Logon> </form> </body> </html> • Using Server-side VB Script:
Problem • Want restricted access: • However, can type home page directly (bypassing password page)
Solution • Need way for: • password page to tell home page • that user logged in OK
Passing Data between pages • Problem • want to protect all pages from unauthorised access • need to store record of successful login • Variables • only persist for duration of page
Passing Data (persistent) • Cookies (not covered in Soft131) • stored on users’ (client) hard drive • persists between sessions • Database/file (covered in next lecture) • stored on server hard drive • persists between sessions
Passing Data (temporary) • Session object • exists for current session • clears if user closes browser • clears after 20 mins of inactivity • Forms and Self Posting • Query Strings • Useful for passing information between pages
Example: Logon - code (v3) LoginFail.aspx Home.aspx <html> <head> <title></title> <% If Request.Form("txtUserName") = "mark" And Request.Form("txtPassWord") = "soft131" Then Session("LoggedIn") = "Yes" Response.Redirect("home.htm") End If %> </head> <body> <p>Sorry, those login details were incorrect. <p>Please try <a href=Logon.htm>again</a> </body> </html> <html> <head> <title>My Home page</title> <% If Session("LoggedIn") <> "Yes" Then Response.Redirect("Logon.htm") End If %> </head> <body> <p>Welcome to my home page.<br> <img src="YouAreHere.jpg" WIDTH="450" HEIGHT="259"> </body> </html> Logon.htm <html> <head> <title></title> </head> <body> <p>Please logon: <form action=LoginFail.aspx method=post> <input name=txtUserName type=text> <input name=txtPassWord type=text> <input name=btnLogon type=submit value=Logon> </form> </body> </html> • Using Session variable:
Maintaining State: Session Object <html> <head> <title>Login</title> </head> <body> <% If Request.Form("txtUserName") = "George" Then Session("LoginOK") = "Yes" Response.Redirect("Home.aspx") Else Session.Abandon() If Request.Form("txtUserName") <> "" Then Response.Write("Invalid user name, please try again.") End If End If %> <p>Please login: <form name="frmLogin" action="Login.asp" method=post> Username:<input name="txtUserName" type="text"><br> Password:<input name="txtPassWord" type="password"><br> <input name="btnLogin" type="submit" value="Login"> </form> </body> </html> Login.aspx • Session variable • all strings • Abandon method • deletes all session variables • Redirect method • redirects browser to specified page
Maintaining State: Session Object Home.aspx <html> <head> <title></title> <% If Session("LoginOK") <> "Yes" Then Response.Redirect("Login.aspx") End If %> </head> <body> <center><b>Home Page</b></center> <p>Welcome to my home page. </body> </html> ASP code tocheck forsuccessful login
Maintaining State: Self Posting • Form points to self: • If any submitbutton pressedpage re-loads Multiply.aspx <html> <head> <title>Multiply</title> … </head> <body> <form action=Multiply.aspx method=post> … </form> </body> </html>
Example: Multiply do whenbutton clicked Post to Self Multiply.aspx <html> <head> <title>Multiply</title> <% Dim tmpRes Dim tmpNum1 Dim tmpNum2 If Request.Form("btnCalc") <> ""Then tmpNum1 = CDbl(Request.Form("txtNum1")) tmpNum2 = CDbl(Request.Form("txtNum2")) tmpRes = tmpNum1 * tmpNum2 End If %> </head> <body> <form name="frmDefault" action=Multiply.aspx method=post> <p><input name=txtNum1 type=text size=5 maxlength=5 value=<%=tmpNum1%>> <input name=txtNum2 type=text size=5 maxlength=5 value=<%=tmpNum2%>> <p><input name=btnCalc type=submit value=Calc> </form> <p><%=tmpRes%> </body> </html>
Maintaining State: Query Strings Query String • Data added to end of URL (address):http://localhost/page.asp?Surname=Bob • ASP code can use this data: • Request.QueryString("Surname") • would return the value "Bob" • Form method=get • data automatically added to query string
Example: Date-Time Menu.aspx <html> <head> </head> <body> <p>What background colour do you want for you date information? <br><a href=DateTime.aspx?Colour=yellow>Yellow</a> <br><a href=DateTime.aspx?Colour=cyan>Light Blue</a> </body> </html> DateTime.aspx <html> <head> </head> <body bgcolor=<%=request.querystring("Colour")%>> <p>The date is <%=Format(Now(), "D")%>. <p>The time is <%=Format(Now(), "T")%>. </body> </html>
Reference: Server Object Model • Request object: calling web page • Form: used to get form data from page • QueryString: used to get data from address (?) • Response object: web page sent back • Write: used to put text into web page • Redirect: used to navigate to other page • Clear: erases all HTML in web page • Session object: store data between pages • Abandon: clears session data
Example: Apples • SPECIFICATION • User Requirements • help children learn numbers 1 - 10 • Software Requirements • Functional: • display random number of apples (between 1 & 10) • ask child how many apples are there • child enters answer • computer responds appropriately • Non-functionalshould be easy to use, and interesting
Tutorial Exercises • LEARNING OBJECTIVE:pass data between pages using session variables, query strings, and self-posting • Task 1: Get Logon v3 working (from the lecture) • Task 2: Get the Multiply example (from the lecture) working • Task 3: Get the Date-Time example (from the lecture) working • Task 4: Design and code the Apples example