cp3024 lecture 3
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
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
Dynamic Content Generated by the Server
Browser requests pagePage is generated “on the fly”Content depends on:
– Request made– Time of day– Etc.
“On-the-fly” Content
Server
Client
Request
HTML generated “on-the-fly”
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)
Directive Format
<!--#command parameter=“argument”-->
e.g.
<!--#exec cmd=“/bin/finger”-->
Permitted Directives
config echo exec flastmod fsize includeprintenv set
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>
Running echo
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” -->
Running exec
fsize
Inserts the size of a named fileUseful for warning about size of graphics
etc.
fsize file|virtual=“path”
<!--#fsize file=“myphoto.jpeg” -->
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” -->
Running fsize and flastmod
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” -->
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
CGI in action
ProgramClient
Server
HTTP Request
Data for program
Generated HTML
HTML
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
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
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
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
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
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
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
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>
Switching between PHP and HTML
Can be done as and when:<? For($i=0; $i<100; $i++) { ?>
<br>
<?}?>
PHP Comments
/* C style comment */// C++ style comments# Unix shell style comments
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
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
String Handling
Characters within strings can be obtained via subscripting
Subscripts start at 0
$hello = “Hello World”
$hello[1] will be “e”
Outputting A String
The function echo() outputs a string to the standard output
e.g. echo(“Hello World”);
echo($astring)
Finding the length of a string
The function strlen() returns a string’s length
$hello=“Hello World”
The value of strlen($hello) is 11
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
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
Control Structures
Standard syntax for creating loops– if– switch– while– do/while– for
if statement
if (expression) {statements
}elseif (expression) {
statements}else {
statements}
Conditional expressions
Standard comparison operators< , <=, >=, >, ==, !=
Note the ==Any numeric expression which has a value
of 0 is false otherwise it is true
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
if Example
if ($var==56){echo “It’s the same”;
}elseif($var<56){echo “Less than”;
}else{echo “Greater than”;
}
for loop
for (start_expr; cond_expr; iter_expr){
statements
}Executes the statements while the
cond_expr is true
for example
for($var=0; $var<=12; $var++){
echo($var);
}
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
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>
Simple Form Output
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>
Output
Getting It On the Server
Treat PHP scripts like ordinary HTML pages
Except save in files called .phpSuitably equipped Web Server does the
rest
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)
Summary
Dynamic pages generated by the server– Server Side Includes– Common Gateway Interface– PHP