Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simple PHP application. A simple application We are going to develop a simple PHP application with a Web interface. The user enters two numbers and the.

Similar presentations


Presentation on theme: "Simple PHP application. A simple application We are going to develop a simple PHP application with a Web interface. The user enters two numbers and the."— Presentation transcript:

1 Simple PHP application

2 A 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

3 UML 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

5 User-script interaction Browser Web server get multiply.php?x=5&y=6 Locate file and run as PHP multiply.php PHP processor run script “5 * 6 = 30” 5 * 6 = 30 HTML MIME User Enter data Read output get form.htm Click link Locate file Calculate button x=5 y=6 Display form.htm Display generated HTML page

6 User-browser interaction Browser User Enter data Read output Click link Calculate button Display form.htm Display generated HTML page

7 User – Browser interaction User locates desired link Multiply numbers 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

8 form.htm <!-- this is not XHTML standard because it lacks headers but it does have all attribute values in quotes and all tags are terminated --> x = y =

9 Browser-Server interaction Browser Web server get multiply.php?x=5&y=6 Locate file and run as PHP 5 * 6 = 30 Click button

10 Browser-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 METHOD=GET in an HTML Form –appended to the ACTION URL Explicitly added to the base URL e.g. – 5 * 6 multiply.php?x=5&y=6 –A separate document (invisible to user) METHOD=POST 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

11 Server-script interaction Web server get multiply.php?x=5&y=6 Locate file and run as PHP multiply.php run script “5 * 6 = 30” x=5 y=6

12 User-script interaction Browser Web server get multiply.php?x=5&y=6 Locate file and run as PHP multiply.php PHP processor run script “5 * 6 = 30” 5 * 6 = 30 HTML MIME User Enter data Read output get form.htm Click link Locate file Calculate button x=5 y=6 Display form.htm Display generated HTML page

13 form.htm <!-- this is not XHTML standard because it lacks headers but it does have all attribute values in quotes and all tags are terminated --> x = y =

14 Browser-Server interaction Browser Web server get multiply.php?x=5&y=6 Locate file and run as PHP 5 * 6 = 30 HTML MIME Click button

15 URL Relative URL –multiply.php?x=5&y=6 Absolute URL –http://localhost/UFIE8V/l3php/multiply.php?x=5&y=6 (my Home machine running IIS)http://localhost/UFIE8V/l3php/multiply.php?x=5&y=6 –http://stocks.uwe.ac.uk/~cjwallac/UFIE8V/l3php/multiply.php?x=5&y=6 (the development server)http://stocks.uwe.ac.uk/~cjwallac/UFIE8V/l3php/multiply.php?x=5&y=6 –http://www.uwe.ac.uk/~cjwallac/UFIE8V/l3php/multiply.p hp?x=5&y=6 (the public production server)http://www.uwe.ac.uk/~cjwallac/UFIE8V/l3php/multiply.p hp?x=5&y=6

16 Server-script interaction Web server get multiply.php?x=5&y=6 Locate file and run as PHP multiply.php PHP processor run script “5 * 6 = 30” x=5 y=6

17 Server-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 $_GET[‘x’] 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 – 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

18 Multiply.php script <?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"; ?>

19 The 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 5 * 6 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

20 Multiply2.php Program composition with ‘require’ PHP introduction - multiply script PHP introduction

21 ‘Sticky 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 – Calculate Product Calculate Product

22

23 multiply3.php Combined form and calculator <?php // first compute the output, but only if data has been input if( isset($calc)) { // data was submitted $prod = $x * $y; } else { // set defaults $x=0;$y=0;$prod=0; } ?> "> x = "/> y= "/> x * y =

24 Initial Form x = y= x * y = 0

25 Form with Entered Values x = y= x * y = 5888889

26 Forms processing Interface and presentation can be easily improved – Calculate Product Calculate Product 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

28 SMS version Now nearly the same application with an SMS presentation layer. For variety, I’ve 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

29 SMS to script interface Script ‘plugs’ into SMS server Must obey the protocol: –Parameters Text From Code –Reply text: Reply: Entry required in routing file: –MUL http://www.cems.uwe.ac.uk/~cjwallac/ISD3/sms mult.php http://www.cems.uwe.ac.uk/~cjwallac/ISD3/sms mult.php

30 smsmult.php <?php $text=trim($text); // to remove trailing whitespace $nums = split(" +",$text); // split the string apart on spaces // + means 1 or more occurances $prod=1; foreach ($nums as $number){ $prod=$prod*$number; // accumulate the product } $numlist = join(" x ",$nums); // join the numbers with ' x ' print "Reply: $numlist = $prod"; ?>


Download ppt "Simple PHP application. A simple application We are going to develop a simple PHP application with a Web interface. The user enters two numbers and the."

Similar presentations


Ads by Google