1 / 26

10 – Passing Data between pages: Sessions, Query Strings, & Self Posting

10 – 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:

anais
Download Presentation

10 – Passing Data between pages: Sessions, Query Strings, & Self Posting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 10 – Passing Data between pages: Sessions, Query Strings, & Self Posting

  2. 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

  3. IIS • Put ASP pages in: • C:\INetPub\wwwRoot(this part of hard disk exposed to outside world) • Execute pages by putting: • localhost(in web browser, e.g. IE, means local machine) • ASP pages don't work by double-clicking

  4. IIS – Date.asp C:\INetPub\wwwRoot\Test\Date.asp localhost/test/date.asp Short for Response.Write

  5. Example: Logon • Restrict access tohome page

  6. Example: Logon - code (v2) LoginFail.asp 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.asp 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:

  7. Problem • Want restricted access: • However, can type home page directly (bypassing password page)

  8. Solution • Need way for: • password page to tell home page • that user logged in OK

  9. 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

  10. 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

  11. 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

  12. Example: Logon - code (v3) LoginFail.asp Home.asp <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.asp 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:

  13. Maintaining State: Session Object <html> <head> <title>Login</title> </head> <body> <% If Request.Form("txtUserName") = "George" Then Session("LoginOK") = "Yes" Response.Redirect "Home.asp" 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.asp • Session variable • all strings • Abandon method • deletes all session variables • Redirect method • redirects browser to specified page

  14. Maintaining State: Session Object Home.asp <html> <head> <title></title> <% If Session("LoginOK") <> "Yes" Then Response.Redirect "Login.asp" End If %> </head> <body> <center><b>Home Page</b></center> <p>Welcome to my home page. </body> </html> ASP code tocheck forsuccessful login

  15. Maintaining State: Self Posting • Form points to self: • If any submitbutton pressedpage re-loads Multiply.asp <http> <head> <title>Multiply</title> … </head> <body> <form action=Multiply.asp method=post> … </form> </body> </http>

  16. Example: Multiply Only do calc if first load Post to Self Multiply.asp <http> <head> <title>Multiply</title> <% Dim tmpRes Dim tmpNum1 Dim tmpNum2 If Request.Form("txtNum1") <> "" And Request.Form("txtNum2") <> "" Then tmpNum1 = CDbl(Request.Form("txtNum1")) tmpNum2 = CDbl(Request.Form("txtNum2")) tmpRes = tmpNum1 * tmpNum2 End If %> </head> <body> <form name="frmDefault" action=Multiply.asp 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> </http>

  17. 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

  18. Example: Date-Time Menu.asp <html> <head> </head> <body> <p>What background colour do you want for you date information? <br><a href=DateTime.asp?Colour=yellow>Yellow</a> <br><a href=DateTime.asp?Colour=cyan>Light Blue</a> </body> </html> DateTime.asp <html> <head> </head> <body bgcolor=<%=request.querystring("Colour")%>> <p>The date is <%=date()%>. <p>The time is <%=time()%>. </body> </html>

  19. 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

  20. Numeric Expressions many people instinctivelyknow these are wrong data • 23 + 11 - a * 2 • 34 + * 12 + x d o o d o d • b + 1 – 21 45 d o d o d d operators

  21. String Expressions data data data data operator operator operator • "What is " & num1 & " times " & num2 • "What is twice " num1 & "?" • "What is 6 minus " & & num & "?"

  22. 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

  23. Design • user interface: • design: • what buttons, text boxes, text do you need? • implement using HTML • how do you create these? • functionality: • design: • what do you want to happen, and when? • implement using VB Script code • how do you do this?

  24. Problem Solving • Imagine working things out by hand • Consider: • what actions you take, andwhen they are done • what information you remember • Computer must mimic these

  25. Solutions • 'which is right?' • both? • better solution • smaller (more elegant) • fewer lines of code • shorter lines of code

  26. Tutorial Exercises • 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

More Related