50
CP3024 Lecture 3 Server Side Facilities

CP3024 Lecture 3 Server Side Facilities. Lecture contents Server side includes Common gateway interface (CGI) PHP Hypertext Preprocessor (PHP) pages

Embed Size (px)

Citation preview

Page 1: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

CP3024 Lecture 3

Server Side Facilities

Page 2: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Lecture contents

Server side includes Common gateway interface (CGI)PHP Hypertext Preprocessor (PHP) pages

Page 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 serverDelivered to browser when requestedPage always remains the same

Page 4: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Dynamic Content Generated by the Server

Browser requests pagePage is generated “on the fly”Content depends on:

– Request made– Time of day– Etc.

Page 5: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

“On-the-fly” Content

Server

Client

Request

HTML generated “on-the-fly”

Page 6: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Server Side Includes(SSI)

Directives placed in an HTML pageWhen page is delivered server inserts extra

informationBrowser only sees final HTML versionNot supported by all server softwarePages have special suffix (normally .shtml)

Page 7: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Directive Format

<!--#command parameter=“argument”-->

e.g.

<!--#exec cmd=“/bin/finger”-->

Page 8: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Permitted Directives

config echo exec flastmod fsize includeprintenv set

Page 9: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

echo

echo var= “environment_variable”Inserts the value of special side include

variables into pagee.g.

<h1>My server is called

<!--#echo var=“SERVER_NAME” -->

</h1>

Page 10: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Running echo

Page 11: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

exec

Used to execute a command or a user program

Best if program generates HTMLCan’t send data to program

exec cmd|cgi=“string”

<!--#exec cmd=“/bin/finger” -->

Page 12: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Running exec

Page 13: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

fsize

Inserts the size of a named fileUseful for warning about size of graphics

etc.

fsize file|virtual=“path”

<!--#fsize file=“myphoto.jpeg” -->

Page 14: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

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” -->

Page 15: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Running fsize and flastmod

Page 16: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

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” -->

Page 17: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Common Gateway Interface

Known as CGIOne of the most misunderstood Web

technologiesAllows client to pass data to programs

running on serverPrograms generate HTML to be returned to

browser

Page 18: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

CGI in action

ProgramClient

Server

HTTP Request

Data for program

Generated HTML

HTML

Page 19: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

What is a CGI program?

Can be written in any languageCGI defines the format of the data passed to the

programProgram reads data from an environment variable

called QUERY_STRINGProgram generates output prefixed by Content-

type: header. E.g.:echo Content-type: text/html echo

Page 20: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

URL encoding

Data may be sent via GET or POSTValues are encoded as variable/value pairsEach pair is separated by &

– firstname=Joe&lastname=Bloggs

CGI program must decode this stringGET strings go into the server logs but

POST strings are not logged

Page 21: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

CGI Environment Variables

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

Page 22: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

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

Page 23: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Load On Server

Originally each CGI script ran as a separate process

Very costly on server resourcesNewer mechanisms run scripts in threadsServer designers ensure that CGI routines

cannot crash the Web server

Page 24: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Server-side Scripting

CGI programs generate HTMLServer side script languages embed code

within HTMLServer executes code before delivering to

browserExamples include PHP, ASP, JSP and XSPRequires additional server software

Page 25: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

PHP

PHP Hypertext PreprocessorServer-side, HTML-embedded, cross-

platform scripting languageAvailable as Unix Apache moduleCGI version works with IIS on MS

WindowsSimilar to C or Perl

Page 26: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

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>

Page 27: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Switching between PHP and HTML

Can be done as and when:<? For($i=0; $i<100; $i++) { ?>

<br>

<?}?>

Page 28: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

PHP Comments

/* C style comment */// C++ style comments# Unix shell style comments

Page 29: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Variables in PHP

All names begin with $ e.g. $variableAlphabetic character or underscore (_)

must follow $Remaining chars are alphanumeric or

underscoreNames are case-sensitive $A is not $aTypes determined by first assignment

Page 30: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

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

Page 31: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

String Handling

Characters within strings can be obtained via subscripting

Subscripts start at 0

$hello = “Hello World”

$hello[1] will be “e”

Page 32: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Outputting A String

The function echo() outputs a string to the standard output

e.g. echo(“Hello World”);

echo($astring)

Page 33: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Finding the length of a string

The function strlen() returns a string’s length

$hello=“Hello World”

The value of strlen($hello) is 11

Page 34: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Getting a number from a string

Use the string in a calculationPHP converts as much as it can to a number

$var=“1234”

$num=$var[2] + 5

$num contains the number 8

Page 35: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

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 precedenceStatements separated by semi-colonsExpressions evaluate to true or false

Page 36: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Control Structures

Standard syntax for creating loops– if– switch– while– do/while– for

Page 37: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

if statement

if (expression) {statements

}elseif (expression) {

statements}else {

statements}

Page 38: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Conditional expressions

Standard comparison operators< , <=, >=, >, ==, !=

Note the ==Any numeric expression which has a value

of 0 is false otherwise it is true

Page 39: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

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

Page 40: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

if Example

if ($var==56){echo “It’s the same”;

}elseif($var<56){echo “Less than”;

}else{echo “Greater than”;

}

Page 41: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

for loop

for (start_expr; cond_expr; iter_expr){

statements

}Executes the statements while the

cond_expr is true

Page 42: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

for example

for($var=0; $var<=12; $var++){

echo($var);

}

Page 43: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Getting Data From The Client

Data is often supplied to server side programs via HTML forms

Indicated by the <form> tagSpecifies HTTP method and field namesField names become variables in PHP

scripts– Field name myfield becomes $myfield

Page 44: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

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>

Page 45: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Simple Form Output

Page 46: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

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>

Page 47: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Output

Page 48: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Getting It On the Server

Treat PHP scripts like ordinary HTML pages

Except save in files called .phpSuitably equipped Web Server does the

rest

Page 49: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Other Resources

http://www.php.net http://www.phpbuilder.com/getit/ http://www.zend.com/ http://www.scit.wlv.ac.uk/appdocs/phpChapter 29 (PHP) - Internet & World

Wide Web – How to Program, H.M.Deitel, P.J.Deitel & T.R.Nieto (2nd edition)

Page 50: CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages

Summary

Dynamic pages generated by the server– Server Side Includes– Common Gateway Interface– PHP