View
26
Download
0
Category
Preview:
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
Mark Dixon Page 1
19 – Passing Data between pages:Forms, Sessions, & Query Strings
Mark Dixon Page 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
Mark Dixon Page 3
Example: Logon v2 (design)• Restrict access to
home page
Mark Dixon Page 4
Example: Logon v2 (code)<?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>
Logon.php
<html> <head><title>My Home page</title></head> <body> <p> Welcome to my home page.<br /> <img src="YouAreHere.jpg" /> </p> </body></html>
Home.htm
Mark Dixon Page 5
Example: Logon (Fixed Problem)• View Source – shows client-side script:
No server-side code
Mark Dixon Page 6
Example: Logon (Problem 2)• User can type home page url (address)
directly (bypassing logon page)
Mark Dixon Page 7
Solution• Need way for:
– password page to tell home page
– that user logged in OK
Mark Dixon Page 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
Mark Dixon Page 9
Example: Logon v3 (code)
<?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>
Logon3.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>
Home3.php
• Does not work: always redirect to logon
LogonOKTrue
Mark Dixon Page 10
Example: Logon v3 (Error)• Variables – don't persist between pages
Mark Dixon Page 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
Mark Dixon Page 12
Maintaining State: Session Object<?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>
Send.php
• Start Session
• Session variable– no declaration
Mark Dixon Page 13
Maintaining State: Session Object
<?php session_start(); $Msg = $_SESSION["MSG"];?>
<html> <head><title></title></head> <body> <p> <?php echo $Msg; ?> </p> </body></html>
Display.php
• read session variable, and display in parMsg
Mark Dixon Page 14
Example: Message• Using Session variable:
<?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>
Send.php
<?php session_start(); $Msg = $_SESSION["MSG"];?>
<html> <head><title></title></head> <body> <p> <?php echo $Msg; ?> </p> </body></html>
Display.php
MSGMeet in BGB202
Mark Dixon Page 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";
}
Mark Dixon Page 16
Passing Data (temporary)
• Query Strings– Useful for passing information between pages
via links
Mark Dixon Page 17
Maintaining State: Query Strings• 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
Query String
Mark Dixon Page 18
Example: Date-Time<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>
Menu.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>
DateTime.php
Mark Dixon Page 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
Mark Dixon Page 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
Mark Dixon Page 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
Mark Dixon Page 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
Mark Dixon Page 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.
Recommended