Title: Simple PHP application
1Simple PHP application
2A simple application
- We are going to develop a simple PHP application
with a Web interface. - The user enters two numbers and the application
returns the two multiplied to together - Start
3UML Interaction Diagram
- Interaction diagrams describe the communication
between objects, but we will use them to describe
interation between programs even if they are not
objects
4- Simple interaction diagram
- Simple example and explanation
- Ref to UML
5User-script interaction
User
Browser
Web server
multiply.php PHP processor
Click link
page
get form.htm
Locate file
Enter data
Display form.htm
Calculate button
get multiply.php?x5y6
Locate file and run as PHP
x5 y6
run script
5 6 30 HTML MIME
5 6 30
Read output
Display generated HTML
6User-browser interaction
User
Browser
Click link
page
Enter data
Display form.htm
Calculate button
Read output
Display generated HTML
7User Browser interaction
- User locates desired link
- lta hrefform.htmgt Multiply numberslt/agt
- Browser retrieves the form from the server
- Form is displayed
- User fills in the fields on the form
- User clicks submit button
- Magic stuff happens
- User reads the result and goes back to the form
to change the values in the form
8form.htm
- lt!--
- this is not XHTML standard because it lacks
headers - but it does have all attribute values in quotes
and all tags are terminated - --gt
- ltform method"get" action"multiply.php"gt
- x ltinput typetext name"x" size"5"/gt
- y ltinput typetext name"y" size"5"/gt
- ltinput type"submit" value"Calculate"/gt
- lt/formgt
9Browser-Server interaction
Browser
Web server
Click button
get multiply.php?x5y6
Locate file and run as PHP
5 6 30
10Browser-Server interaction
- Parameters passed as data couplets (name/value
pairs) either - Attached to URL (and visible to user in the
address line) - Values must be URL-Encoded
- space to
- punctuation to hex e.g. ? to 3f
- METHODGET in an HTML Form
- appended to the ACTION URL
- Explicitly added to the base URL e.g.
- lta hrefmultiply.php?x5y6gt5 6lt/agt
- A separate document (invisible to user)
- METHODPOST in an HTML Form
- Result passed back in a MIME wrapper
- MIME tells browser what kind of file it is
(HTML,JPEG, XML, Flash.. ) - Content-type image/jpeg
11Server-script interaction
Web server
multiply.php
get multiply.php?x5y6
Locate file and run as PHP
x5 y6
run script
5 6 30
12User-script interaction
User
Browser
Web server
multiply.php PHP processor
Click link
page
get form.htm
Locate file
Enter data
Display form.htm
Calculate button
get multiply.php?x5y6
Locate file and run as PHP
x5 y6
run script
5 6 30 HTML MIME
5 6 30
Read output
Display generated HTML
13form.htm
- lt!--
- this is not XHTML standard because it lacks
headers - but it does have all attribute values in quotes
and all tags are terminated - --gt
- ltform method"get" action"multiply.php"gt
- x ltinput typetext name"x" size"5"/gt
- y ltinput typetext name"y" size"5"/gt
- ltinput type"submit" value"Calculate"/gt
- lt/formgt
14Browser-Server interaction
Browser
Web server
Click button
get multiply.php?x5y6
Locate file and run as PHP
5 6 30 HTML MIME
15URL
- Relative URL
- multiply.php?x5y6
- Absolute URL
- http//localhost8080/calc/multiply.php?x5y6
(my local server) - http//stocks.uwe.ac.uk/cjwallac/apps/calc/multip
ly.php?x5y6 (the development server) - http//www.uwe.ac.uk/cjwallac/apps/calc/multiply.
php?x5y6 (the public production server)
16Server-script interaction
Web server
multiply.php PHP processor
get multiply.php?x5y6
Locate file and run as PHP
x5 y6
run script
5 6 30
17Server-PHP interaction
- Parameters
- GET and POST Parameters available as
- variables of the same name x, y (deprecated
but used in my code ) - in an array _GETx
- depends on the setup
- Parameter values are URL-Decoded
- Implicit Parameters
- In addition to the user-defined parameters, other
data is available about the client and about the
server - PHP_SELF is the program name
- HTTP_USER_AGENT is the browser
- Reply
- HTML goes through to output
- lt?php ?gt script is executed as PHP
- Print or echo statements add to output
- All output returned to Server
- Other kinds of output (JPEG, GIF.. ) require
different headers to be output
18Multiply.php script
- lt?php
- /
- function to multiply the two input numbers
and display the result - input
- x, y numbers
- author
- Chris Wallace
- 9 Oct 204
- /
- // calculate the result
- // no typing or declaring new variable
- // also implicit conversion of string to number
- prod x y
- // values of variables interpolated into the
string - print "x y prod"
- ?gt
19The generated HTML
- 5 6 30
- This simple text is not XHTML compliant of
course. - HTML to provide a readable page can be added
around the PHP - lta hrefmultiply2.php?x5y6gt5 6lt/agt
- Note that we have added the name/value pairs to
the URL not very useful here obviously. - some pairs can be in the action URL of a form,
some in input fields - The script multiply2.php includes the original
PHP script within an HTML page
20Multiply2.phpProgram composition with require
- lthtmlgt
- lttitlegtPHP introduction - multiply scriptlt/titlegt
- lt/headgt
- ltbodygt
- ltimg src"cems-banner.gif"gt
- lth1gtPHP introductionlt/h1gt
- lttablegtlttrgtlttdgtltimg src"multiply.jpg"gtlt/tdgtlttd
width"5"lt/tdgtlttdgtltfont color"red" size"4"gt - lt?php include "multiply.php ?gt
- lt/fontgtlt/tdgtlttrgtlt/tablegt
- lt/bodygt
- lt/htmlgt
21Sticky Forms
- Poor interface -user must to go back to original
form to change the data and re-calculate. - Key idea
- Combine form and calculator into one script
- Do calculation (if required) first
- Put inputs as defaults in form
- Simple combination of form and script
- lta hrefmultiply3.phpgt Calculate Product lt/agt
22(No Transcript)
23multiply3.phpCombined form and calculator
- lt?php
- // first compute the output, but only if data has
been input - if( isset(calc)) // data was submitted
- prod x y
-
- else // set defaults
- x0y0prod0
-
- ?gt
- ltform method"get" action"lt?php print PHP_SELF
?gt"gt - x ltinput typetext name"x" size"5"
value"lt?php print x?gt"/gt - y ltinput typetext name"y" size"5"
value"lt?php print y?gt"/gt - x y lt?php print prod ?gt
- ltbr/gtltinput type"submit" name"calc"
value"Calculate"/gt - lt/formgt
24Initial Form
- ltform method"get" actionscripts/multiply3.php"gt
- x ltinput typetext name"x" size"5"
value"0"/gt - y ltinput typetext name"y" size"5"
value"0"/gt - x y 0ltbr/gtltinput type"submit" name"calc"
value"Calculate"/gt - lt/formgt
25Form with Entered Values
- ltform method"get" actionscripts/multiply3.php"gt
- x ltinput typetext name"x" size"5"
value"654321"/gt - y ltinput typetext name"y" size"5"
value"9"/gt - x y 5888889ltbr/gtltinput type"submit"
name"calc" value"Calculate"/gt - lt/formgt
26Forms processing
- Interface and presentation can be easily improved
- lta hrefmultiply4.phpgt Calculate Product lt/agt
- Script can do validation and add error messages
to the form where fields are missing or invalid. - Where to do the processing?
- on Client in Javascript
- on Server in PHP or Java
- What factors do you need to consider to decide?
27(No Transcript)
28SMS version
- Now nearly the same application with an SMS
presentation layer. - For variety, Ive generalised to allow any number
of numbers to be multiplied together - Text
- MUL num1 num2 num3
- To
- 076 24 80 37 59
- Eg.
- MUL 34 56 78
- Reply
- 34 x 56 x 78 148512
29SMS to script interface
- Script plugs into SMS server
- Must obey the protocol
- Parameters
- Text
- From
- Code
- Reply text
- Reply ltmessage to send backgt
- Entry required in routing file
- MUL http//www.cems.uwe.ac.uk/cjwallac/ISD3/smsmu
lt.php
30smsmult.php
- lt?php
- texttrim(text) // to remove
trailing whitespace - nums split(" ",text) // split the string
apart on spaces - // means 1 or
more occurances - prod1
- foreach (nums as number)
- prodprodnumber // accumulate the
product -
- numlist join(" x ",nums) // join the
numbers with ' x ' - print "Reply numlist prod"
- ?gt