Chapter 7 Server-side dynamic pages

strawberriesfarctateΛογισμικό & κατασκευή λογ/κού

4 Νοε 2013 (πριν από 4 χρόνια και 8 μήνες)

75 εμφανίσεις

Chapter 7
dynamic pages
 Form, GET and POST
 Scripting language: PHP
 What is CGI?
 The Common Gateway Interface (CGI) is a standard protocol for
interfacing external application software with an information server,
commonly a web server.
 The task of such an information server is to respond to request by
returning output. Each time a request is received, the server
analyzes what the request asks for, and return the appropriate
 Two basic methods for the server to do this are the following:
 If the request identifies a file stored on disk, then return the contents of
that file.
 If the request identifies an executable command and possibly
arguments, then run the command and return its output.
 CGI defines a standard way.
how information about server and the request
is passed to the command in the form of
argument and environment variables,
how the command can pass back extra
information about the output (e.g, type) in the
form of headers.
4CGI: common gateway interface
 Common ?
 CGI programs can be written with many languages.
 C, C++, Java, Perl, Visual Basic or,
 any other language which can accept user input, process that
input, and respond with output.
 CGI works with many different types of systems.
 Mac, NT, Windows, OS2, Amiga, UNIX,
 pretty much every operating system which runs web server
 Gateway ?
 CGI as a middleman or a translator whose job is to
help more powerful resources like databases,
graphics generation programs, or network
applications talk to each other and work towards a
common solution.
 CGI is the gateway between the web surfer with his
web browser and the vast web of computers (each
with their own specific language and protocols) and
computer programs (each with their own interfaces
and methods of output).
 CGI translates between the language the client
speaks (perhaps HTMLized English) and the
multitude of languages spoken by the resources the
client wants to utilize (such as SQL for relational
 Interface ?
 CGI is not a language, neither is it a program.
 CGI is a standard of communication, a process, an
interface which provides well-defined rules for
creating partnerships.
 If everyone follows the rules of the interface, then
everyone can talk to each other.
 Typically we say that we write "CGI programs" or
"CGI scripts" which perform the functions of the
common gateway interface.
7CGI processes
8See also
 protocol for interfacing interactive programs with a web server.
FastCGI is a variation on the earlier CGI;
 main aim is to reduce the overhead associated with interfacing
the web server and CGI programs, allowing a server to handle
more web page requests at once.
 an alternative to the CGI protocol.
 It is similar to FastCGI but is designed to be easier to implement.
 Web Server Gateway Interface
 defines a simple and universal interface between web servers
and web applications or frameworks for the Python programming
9Form, GET, POST
 There are two way to send data form a
web form to a CGI program: GET and
GET method:
 input values from the form are sent as part of the
URL and saved in the QUERY_STRING
environment variable
POST method:
 data is sent as an input stream to the program
10Scripting language: PHP
 What is PHP?
PHP stands for PHP: Hypertext Preprocessor
PHP is a server-side scripting language, like
PHP scripts are executed on the server
PHP supports many databases (MySQL,
Informix, Oracle, Sybase, Solid, PostgreSQL,
Generic ODBC, etc.)
PHP is an open source software
PHP is free to download and use
11Scripting language: PHP
 What is a PHP File?
 PHP files can contain text, HTML tags and scripts
 PHP files are returned to the browser as plain HTML
 PHP files have a file extension of ".php", ".php3", or ".phtml"
 Why PHP?
 PHP runs on different platforms (Windows, Linux, Unix, etc.)
 PHP is compatible with almost all servers used today (Apache,
IIS, etc.)
 PHP is FREE to download from the official PHP resource:
 PHP is easy to learn and runs efficiently on the server side
12PHP Basic Syntax
 There are four difference types of PHP tag when
designing PHP pages
 Short type: <? echo “Welcome to PHP.”; ?>
 XML format type: <?php echo ”Welcome to PHP with XML”; ?>
 Script type:
<script language=‘php’>
echo “PHP script”;
 ASP like type:
echo”PHP Script”;
13PHP Basic Syntax
 Each statement ends with semicolon
// a single-line comment
# a single-line comment
/* a large comment block */
 Calling function:
echo “<p><b>Welcome to function</b>”;
echo “<p>Today is:”;
# call date() function
echo date(“H:I, jS F”);
echo “<br>”;
14PHP Basic Syntax
 Access form variables
Form variables: using “$” in front of variable
Two ways to access form data through
 Short style: need to configure register_global index
is “On” (in php.ini file)
 Long style: using array $HTTP_POST_VARS and
$HTTP_GET_VARS, need to configure
register_global index is “Off” and track_vars is “On”
(in php.ini file)
15PHP Basic Syntax
 Identifiers: unique!
 Variables declare: no need, but we should
declare and provide them with initial values
before using.
 Assign values to variables
 Operators: arithmetic, string, assignment,
reference, comparison,…
 @ operator
16PHP Basic Syntax
 Conditional structures
if else
do while
17PHP Basic Syntax - Flat file/ text file
 Open a file:
 $ft=fopen(filename, filemode);
 Filemode: r, r+, w, w+, a, a+
 Write to a file:
 fwrite($ft, $outputstring);
 fputs(int ft, string str, int length);
 Read from a file:
 fgets(), feof(), readfile()
 Close a file:
 Other functions: file_exists(), filesize(), unlink(),
18PHP connect to a database
 We choose MySQL database
Create a connection to a MySQL database
 mysql_connect(servername, username, password);
Optional. Specifies
Optional. Specifies
the username to log
the server to connect
Specifies the
in with. Default value
to. Default value is
password to log
is the name of the
in with. Default
user that owns the
is ""
server process
19PHP connect to a database
 Closing a Connection
 The connection will be closed automatically when the script
ends. To close the connection before, use the mysql_close()
function, for instance:
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
die('Could not connect: ' . mysql_error());
// some code