260 likes | 350 Views
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
E N D
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 • Query Strings • Session Variables
Example: Logon v2 (design) • Restrict access tohome page
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>
Example: Logon (Fixed Problem) • View Source – shows client-side script: No server-side code
Example: Logon (Problem 2) • User can type home page url (address) directly (bypassing logon page)
Solution • Need way for: • password page to tell home page • that user logged in OK
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
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
Example: Logon v3 (Error) • Variables – don't persist between pages
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
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
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
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
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"; }
Passing Data (temporary) • Query Strings • Useful for passing information between pages via links
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
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>
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
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
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
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
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.