1 / 50

CP3024 Lecture 3

CP3024 Lecture 3. Server Side Facilities. Lecture contents. Server side includes Common gateway interface (CGI) PHP Hypertext Preprocessor (PHP) pages. Static HTML Page. Page created and stored on server Delivered to browser when requested Page always remains the same.

willis
Download Presentation

CP3024 Lecture 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CP3024 Lecture 3 Server Side Facilities

  2. Lecture contents • Server side includes • Common gateway interface (CGI) • PHP Hypertext Preprocessor (PHP) pages

  3. Static HTML Page • Page created and stored on server • Delivered to browser when requested • Page always remains the same

  4. Dynamic Content Generated by the Server • Browser requests page • Page is generated “on the fly” • Content depends on: • Request made • Time of day • Etc.

  5. “On-the-fly” Content Server Request Client HTML generated “on-the-fly”

  6. Server Side Includes(SSI) • Directives placed in an HTML page • When page is delivered server inserts extra information • Browser only sees final HTML version • Not supported by all server software • Pages have special suffix (normally .shtml)

  7. Directive Format • <!--#command parameter=“argument”--> e.g. <!--#exec cmd=“/bin/finger”-->

  8. Permitted Directives • config • echo • exec • flastmod • fsize • include • printenv • set

  9. echo • echo var= “environment_variable” • Inserts the value of special side include variables into page e.g. <h1>My server is called <!--#echo var=“SERVER_NAME” --> </h1>

  10. Running echo

  11. exec • Used to execute a command or a user program • Best if program generates HTML • Can’t send data to program exec cmd|cgi=“string” <!--#exec cmd=“/bin/finger” -->

  12. Running exec

  13. fsize • Inserts the size of a named file • Useful for warning about size of graphics etc. fsize file|virtual=“path” <!--#fsize file=“myphoto.jpeg” -->

  14. flastmod • Inserts the last modified date for a named file • Used to indicate how up to date the information is flastmod file=“path” <!--#flastmod file=“index.html” -->

  15. Running fsize and flastmod

  16. include • Use to include text (normally html) into a file • Can be used to add standard text to pages include file|virtual=“path” <!--#include file=“disclaim.html” -->

  17. Common Gateway Interface • Known as CGI • One of the most misunderstood Web technologies • Allows client to pass data to programs running on server • Programs generate HTML to be returned to browser

  18. CGI in action Client Program HTTP Request Data for program HTML Generated HTML Server

  19. What is a CGI program? • Can be written in any language • CGI defines the format of the data passed to the program • Program reads data from an environment variable called QUERY_STRING • Program generates output prefixed by Content-type: header. E.g.: echo Content-type: text/html echo

  20. URL encoding • Data may be sent via GET or POST • Values are encoded as variable/value pairs • Each pair is separated by & • firstname=Joe&lastname=Bloggs • CGI program must decode this string • GET strings go into the server logs but POST strings are not logged

  21. AUTH_TYPE CONTENT_LENGTH CONTENT_TYPE DOCUMENT_ROOT GATEWAY_INTERFACE HTTP_ACCEPT HTTP_COOKIE HTTP_FROM HTTP_REFERER PATH_INFO PATH_TRANSLATED QUERY_STRING REMOTE_ADDR REMOTE_HOST REMOTE_IDENT REMOTE_USER REQUEST_METHOD SCRIPT_NAME SERVER_NAME SERVER_PORT SERVER_PROTOCOL SERVER_SOFTWARE CGI Environment Variables

  22. Identifying CGI Scripts • Some server set-ups require script to be stored in /cgi-bin • Other set-ups allow scripts anywhere • Script names have format ????.cgi • Scripts should be made executable for the server username • Apache server username is nobody

  23. Load On Server • Originally each CGI script ran as a separate process • Very costly on server resources • Newer mechanisms run scripts in threads • Server designers ensure that CGI routines cannot crash the Web server

  24. Server-side Scripting • CGI programs generate HTML • Server side script languages embed code within HTML • Server executes code before delivering to browser • Examples include PHP, ASP, JSP and XSP • Requires additional server software

  25. PHP • PHP Hypertext Preprocessor • Server-side, HTML-embedded, cross-platform scripting language • Available as Unix Apache module • CGI version works with IIS on MS Windows • Similar to C or Perl

  26. Embedding PHP in HTML • Between <? And ?> tags • <?echo “Hello World”;?> • Between <?php and ?> tags for XML • <?php echo “Hello World”; ?> • In <script> tags • <script language=“php”> echo “Hello World”; </script>

  27. Switching between PHP and HTML • Can be done as and when: <? For($i=0; $i<100; $i++) { ?> <br> <?}?>

  28. PHP Comments • /* C style comment */ • // C++ style comments • # Unix shell style comments

  29. Variables in PHP • All names begin with $ e.g. $variable • Alphabetic character or underscore (_) must follow $ • Remaining chars are alphanumeric or underscore • Names are case-sensitive $A is not $a • Types determined by first assignment

  30. Data Types in PHP • Integer • Whole numbers –2,147,483,648 to 2,147,483,647 • Floating Point • Decimal values in the range 1.7E-308 to 1.7E308 • String • Sequence of characters e.g. “Hello World” • Can convert using setype() function or by casting: • settype($myInt, “string”) • (integer) $myString

  31. String Handling • Characters within strings can be obtained via subscripting • Subscripts start at 0 $hello = “Hello World” $hello[1] will be “e”

  32. Outputting A String • The function echo() outputs a string to the standard output e.g. echo(“Hello World”); echo($astring)

  33. Finding the length of a string • The function strlen() returns a string’s length $hello=“Hello World” The value of strlen($hello) is 11

  34. Getting a number from a string • Use the string in a calculation • PHP converts as much as it can to a number $var=“1234” $num=$var[2] + 5 $num contains the number 8

  35. Expressions • Includes normal arithmetic operations • $i=$i+1 or $i++ • $i=$i-1 or $i-- • Also / (divide) and * (multiply) • % is the modulo operation • May use brackets to specify precedence • Statements separated by semi-colons • Expressions evaluate to true or false

  36. Control Structures • Standard syntax for creating loops • if • switch • while • do/while • for

  37. if statement • if (expression) { statements } elseif (expression) { statements } else { statements }

  38. Conditional expressions • Standard comparison operators < , <=, >=, >, ==, != • Note the == • Any numeric expression which has a value of 0 is false otherwise it is true

  39. Can combine conditiond • Logical connectors && and || • ($var<6) && ($var>4) • Variable is less than 6 AND greater than 4 • ($var==6) || ($var==4) • Variable is equal to 4 OR equal to 6

  40. if Example if ($var==56){ echo “It’s the same”; } elseif($var<56){ echo “Less than”; } else{ echo “Greater than”; }

  41. for loop • for (start_expr; cond_expr; iter_expr){ statements } • Executes the statements while the cond_expr is true

  42. for example for($var=0; $var<=12; $var++){ echo($var); }

  43. Getting Data From The Client • Data is often supplied to server side programs via HTML forms • Indicated by the <form> tag • Specifies HTTP method and field names • Field names become variables in PHP scripts • Field name myfield becomes $myfield

  44. A Simple Form <html> <head> <title>Multiply Form</title> </head> <body><h1>Enter multiplier</h1> <form action="multiply.php" method="POST"> <input type="text" name="multiplier"> </form></body> </html>

  45. Simple Form Output

  46. PHP Script <html> <head><title>Times Table</title></head> <body> <?for($i=1;$i<=12;$i++){ echo($i); ?> * <?echo($multiplier);?> = <?echo($multiplier*$i);?> <br> <?}?> </body> </html>

  47. Output

  48. Getting It On the Server • Treat PHP scripts like ordinary HTML pages • Except save in files called .php • Suitably equipped Web Server does the rest

  49. Other Resources • http://www.php.net • http://www.phpbuilder.com/getit/ • http://www.zend.com/ • http://www.scit.wlv.ac.uk/appdocs/php • Chapter 29 (PHP) - Internet & World Wide Web – How to Program, H.M.Deitel, P.J.Deitel & T.R.Nieto (2nd edition)

  50. Summary • Dynamic pages generated by the server • Server Side Includes • Common Gateway Interface • PHP

More Related