Lecture5

advertisement
CP3024 Lecture 5
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
Dynamic Content Generated by
the Server
Browser requests page
Page is generated “on the fly”
Content depends on:
– Request made
– Time of day
– Etc.
“On-the-fly” Content
Request
Client
HTML
generated “onthe-fly”
Server
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)
Directive Format
<!--#command parameter=“argument”-->
e.g. <!--#exec cmd=“/bin/finger”-->
Permitted Directives
 config
 echo
 exec
 flastmod
 fsize
 include
 printenv
 set
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>
Running echo
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” -->
Running exec
fsize
Inserts the size of a named file
Useful 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 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
CGI in action
Client
Program
HTTP
Request
Data for
program
HTML
Generated
HTML
Server
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
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
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 resources
Newer mechanisms run scripts in threads
Server designers ensure that CGI routines
cannot crash the Web server
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
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
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. $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
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”
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 calculation
 PHP 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 precedence
Statements separated by semi-colons
Expressions 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> tag
Specifies HTTP method and field names
Field 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 .php
Suitably 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/php
Summary
Dynamic pages generated by the server
– Server Side Includes
– Common Gateway Interface
– PHP
Download