19 Passing Data between pages Forms Sessions Query
19 – Passing Data between pages: Forms, Sessions, & Query Strings Mark Dixon Page 1
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 Mark Dixon Page 2
Example: Logon v 2 (design) • Restrict access to home page Mark Dixon Page 3
Example: Logon v 2 (code) Logon. php <? php $msg = ""; if(isset($_POST["btn. Logon"])){ $un = $_POST["txt. User. Name"]; $pw = $_POST["txt. Pass. Word"]; if($un == "mark" && $pw == "soft 131"){ header("Location: Home. htm"); }else{ $msg = "Login details incorrect. "; } } ? > <html> <head><title></head> <body> <form method="post"> Please logon: <input name="txt. User. Name" type="text" /> <input name="txt. Pass. Word" type="text" /> <input name="btn. Logon" type="submit" value="Logon" /> <p> <? php echo $msg; ? > </p> </form> </body> </html> Mark Dixon Home. htm <html> <head><title>My Home page</title></head> <body> <p> Welcome to my home page. <img src="You. Are. Here. jpg" /> </p> </body> </html> Page 4
Example: Logon (Fixed Problem) • View Source – shows client-side script: No server-side code Mark Dixon Page 5
Example: Logon (Problem 2) • User can type home page url (address) directly (bypassing logon page) Mark Dixon Page 6
Solution • Need way for: – password page to tell home page – that user logged in OK Mark Dixon Page 7
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 Mark Dixon Page 8
Example: Logon v 3 (code) Logon 3. php <? php $Login. OK = False; $msg = ""; if(isset($_POST["btn. Logon"])){ $un = $_POST["txt. User. Name"]; $pw = $_POST["txt. Pass. Word"]; if($un == "mark" && $pw == "soft 131"){ $Login. OK = True; header("Location: Home. htm"); }else{ $msg = "Login details incorrect. "; } } ? > <html> <head><title></head> <body> <form method="post"> Please logon: <input name="txt. User. Name" type="text" /> <input name="txt. Pass. Word" type="text" /> <input name="btn. Logon" type="submit" value="Logon" /> <p> <? php echo $msg; ? > </p> </form> </body> </html> Mark Dixon Home 3. php Logon. OK True <? php if($Login. OK != True){ header("Location: Login 3. php"); } ? > <html> <head><title>My Home page</title></head> <body> <p> Welcome to my home page. <img src="You. Are. Here. jpg" /> </p> </body> </html> • Does not work: always redirect to logon Page 9
Example: Logon v 3 (Error) • Variables – don't persist between pages Mark Dixon Page 10
Passing Data (temporary) • Session object – used to pass information between pages: session_start(); $_SESSION["Thing"] = 91 Put 91 into Thing – exists for current session – persist between pages – clears if user closes browser – clears after 20 mins of inactivity – no need for declaration Mark Dixon Page 11
Maintaining State: Session Object Send. php <? php session_start(); if(isset($_POST["btn. Send"])){ $_SESSION["MSG"] = "Meet in BGB 202"; }else{ if(isset($_POST["btn. Clear"])){ $_SESSION["MSG"] = ""; } } ? > • Start Session • Session variable – no declaration <html> <head><title></head> <body> <form method="post"> <input name="btn. Send" type="submit" value="Send" /> <input name="btn. Clear" type="submit" value="Clear" /> <p><a href="Display. php">Display</a></p> </form> </body> </html> Mark Dixon Page 12
Maintaining State: Session Object Display. php <? php session_start(); $Msg = $_SESSION["MSG"]; ? > • read session variable, and display in par. Msg <html> <head><title></head> <body> <p> <? php echo $Msg; ? > </p> </body> </html> Mark Dixon Page 13
Example: Message • Using Session variable: Send. php <? php session_start(); if(isset($_POST["btn. Send"])){ $_SESSION["MSG"] = "Meet in BGB 202"; }else{ if(isset($_POST["btn. Clear"])){ $_SESSION["MSG"] = ""; } } ? > <html> <head><title></head> <body> <form method="post"> <input name="btn. Send" type="submit" value="Send" /> <input name="btn. Clear" type="submit" value="Clear" /> <p><a href="Display. php">Display</a></p> </form> </body> </html> Mark Dixon MSG Meet in BGB 202 Display. php <? php session_start(); $Msg = $_SESSION["MSG"]; ? > <html> <head><title></head> <body> <p> <? php echo $Msg; ? > </p> </body> </html> Page 14
Questions: Session Variables • Write a line of Ph. P code to put the number 74 into a session variable called id. $_SESSION["id"] = 74; • Write Ph. P code that displays 'Hello' if the session variable called id is equal to 74 if($_SESSION["id"] == 74){ echo "Hello"; } Mark Dixon Page 15
Passing Data (temporary) • Query Strings – Useful for passing information between pages via links Mark Dixon Page 16
Maintaining State: Query Strings • Data added to end of URL (address): http: //localhost/page. php? Surname=Bob Query String • php code can use this data: – $_GET["Surname"] • would return the value "Bob" • Form method=get – data automatically added to query string Mark Dixon Page 17
Example: Date-Time Menu. php <html> <head><title></head> <body> <p>What background colour do you want for you date information? <a href=Date. Time. php? Colour=yellow>Yellow</a> <a href=Date. Time. php? Colour=cyan>Light Blue</a> </p> </body> </html> Date. Time. php <html> <head><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> Mark Dixon Page 18
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 Mark Dixon Page 19
Tutorial Exercise: Message • LEARNING OBJECTIVE: pass data between pages using session variables, and (form) selfposting • 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 Mark Dixon Page 20
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 Mark Dixon Page 21
Tutorial Exercise: Logon • LEARNING OBJECTIVE: pass data between pages using session variables, and (form) selfposting • Task 1: Type in the code for the Logon v 3 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 Mark Dixon Page 22
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. Mark Dixon Page 23
- Slides: 23