1 / 23

19 – Passing Data between pages: Forms, Sessions, & Query Strings

19 – Passing Data between pages: Forms, Sessions, & Query Strings. 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

merton
Download Presentation

19 – Passing Data between pages: Forms, Sessions, & Query Strings

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. 19 – Passing Data between pages:Forms, Sessions, & Query Strings

  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. Example: Logon v2 (design) • Restrict access tohome page

  4. Example: Logon v2 (code) Home.htm <html> <head><title>My Home page</title></head> <body> <p> Welcome to my home page.<br /> <img src="YouAreHere.jpg" /> </p> </body> </html> Logon.php <?php $msg = ""; if(isset($_POST["btnLogon"])){ $un = $_POST["txtUserName"]; $pw = $_POST["txtPassWord"]; if($un == "mark" && $pw == "soft131"){ header("Location: Home.htm"); }else{ $msg = "Login details incorrect."; } } ?> <html> <head><title></title></head> <body> <form method="post"> Please logon:<br /> <input name="txtUserName" type="text" /><br /> <input name="txtPassWord" type="text" /><br /> <input name="btnLogon" type="submit" value="Logon" /> <p> <?php echo $msg; ?> </p> </form> </body> </html>

  5. Example: Logon (Fixed Problem) • View Source – shows client-side script: No server-side code

  6. Example: Logon (Problem 2) • User can type home page url (address) directly (bypassing logon page)

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

  8. Technique: Dead-Drop Variables • 2 Spies wish to pass message between each other without actually meeting • Arrange a dead-drop location • one spy leaves message at location • other spy visits location later to pick up message • Variables used as dead-drop containers

  9. Example: Logon v3 (code) Home3.php <?php if($LoginOK != True){ header("Location: Login3.php"); } ?> <html> <head><title>My Home page</title></head> <body> <p> Welcome to my home page.<br /> <img src="YouAreHere.jpg" /> </p> </body> </html> Logon3.php <?php $LoginOK = False; $msg = ""; if(isset($_POST["btnLogon"])){ $un = $_POST["txtUserName"]; $pw = $_POST["txtPassWord"]; if($un == "mark" && $pw == "soft131"){ $LoginOK = True; header("Location: Home.htm"); }else{ $msg = "Login details incorrect."; } } ?> <html> <head><title></title></head> <body> <form method="post"> Please logon:<br /> <input name="txtUserName" type="text" /><br /> <input name="txtPassWord" type="text" /><br /> <input name="btnLogon" type="submit" value="Logon" /> <p> <?php echo $msg; ?> </p> </form> </body> </html> • Does not work: always redirect to logon LogonOK True

  10. Example: Logon v3 (Error) • Variables – don't persist between pages

  11. Passing Data (temporary) • Session object • used to pass information between pages: • exists for current session • persist between pages • clears if user closes browser • clears after 20 mins of inactivity • no need for declaration session_start(); $_SESSION["Thing"] = 91 Put 91 into Thing

  12. Maintaining State: Session Object Send.php <?php session_start(); if(isset($_POST["btnSend"])){ $_SESSION["MSG"] = "Meet in BGB202"; }else{ if(isset($_POST["btnClear"])){ $_SESSION["MSG"] = ""; } } ?> <html> <head><title></title></head> <body> <form method="post"> <input name="btnSend" type="submit" value="Send" /> <input name="btnClear" type="submit" value="Clear" /> <p><a href="Display.php">Display</a></p> </form> </body> </html> • Start Session • Session variable • no declaration

  13. Maintaining State: Session Object Display.php <?php session_start(); $Msg = $_SESSION["MSG"]; ?> <html> <head><title></title></head> <body> <p> <?php echo $Msg; ?> </p> </body> </html> • read session variable, and display in parMsg

  14. Example: Message Display.php <?php session_start(); $Msg = $_SESSION["MSG"]; ?> <html> <head><title></title></head> <body> <p> <?php echo $Msg; ?> </p> </body> </html> Send.php <?php session_start(); if(isset($_POST["btnSend"])){ $_SESSION["MSG"] = "Meet in BGB202"; }else{ if(isset($_POST["btnClear"])){ $_SESSION["MSG"] = ""; } } ?> <html> <head><title></title></head> <body> <form method="post"> <input name="btnSend" type="submit" value="Send" /> <input name="btnClear" type="submit" value="Clear" /> <p><a href="Display.php">Display</a></p> </form> </body> </html> • Using Session variable: MSG Meet in BGB202

  15. Questions: Session Variables • Write a line of PhP code to put the number 74 into a session variable called id. • Write PhP code that displays 'Hello' if the session variable called id is equal to 74 $_SESSION["id"] = 74; if($_SESSION["id"] == 74){ echo "Hello"; }

  16. Passing Data (temporary) • Query Strings • Useful for passing information between pages via links

  17. Maintaining State: Query Strings Query String • Data added to end of URL (address):http://localhost/page.php?Surname=Bob • php code can use this data: • $_GET["Surname"] • would return the value "Bob" • Form method=get • data automatically added to query string

  18. Example: Date-Time Menu.php <html> <head><title></title></head> <body> <p>What background colour do you want for you date information? <br /><a href=DateTime.php?Colour=yellow>Yellow</a> <br /><a href=DateTime.php?Colour=cyan>Light Blue</a> </p> </body> </html> DateTime.php <html> <head><title></title></head> <body bgcolor=<?php echo $_GET["Colour"]; ?>> <p>The date is <?php echo date("D d M Y"); ?>. <p>The time is <?php echo date("H:i"); ?>. </body> </html>

  19. Passing Data (persistent) • Cookies (not covered in this module) • stored on users’ (client) hard drive • persists between sessions • Database/file (covered in later lectures) • stored on server hard drive • persists between sessions

  20. Tutorial Exercise: Message • LEARNING OBJECTIVE:pass data between pages using session variables, and (form)self-posting • Task 1: Get the message example working (from the lecture) • Task 2: Change the send.php page so that when you click the buttons it gives some feedback as to what has happened. hint: add a paragraph

  21. Tutorial Exercise: Apples • LEARNING OBJECTIVE:pass data between pages using session variables, and (form)self-posting • Task 1: Add a score facility to the Apples example from last week. • when the page loads, the score should be 0 • when the answer is correct, the score should increase by 1 • when the score goes over 10, a congratulations message should be shown, and the score reset to 0

  22. Tutorial Exercise: Logon • LEARNING OBJECTIVE:pass data between pages using session variables, and (form)self-posting • Task 1: Type in the code for the Logon v3 example (from the lecture) NOTE: this will not work properly (variables do not persist between pages) • Task 2: Modify this to use a session variable to 'remember' whether the logon was successful. Note: It should not be possible to view the source code Note: It should not be possible to bypass the logon

  23. Tutorial Exercise: Date • LEARNING OBJECTIVE:pass data between pages using query strings • Task 1: Get the Date-Time example (from the lecture) working • Task 2: Modify your page to provide another choice of background colour.

More Related