Dynamic Web Pages
Derek Bridge
Department of Computer Science,
University College Cork
Dynamic Web Pages
Aims:
- to know terminology about programs
- to know the difference between static and dynamic web pages
- to understand a simple PHP script
- to understand how the server and the PHP processor handle PHP scripts
Terminology
- Algorithm: definition from CS1105
- We can express an algorithm
- in English (Class exercise: What's the problem?)
- in pseudocode (structured English) (Class exercise:
Is there still a problem?)
- as a program by coding it in one of many
programming languages (e.g. PHP, Java, C, C++, JavaScript,...)
- The program can then be executed or run by a computer
PHP: our programming language for first-year students
- PHP originally
- stood for "Personal Home Page" Tools
- developed in 1994 by Rasmus Lerdorf for logging accesses to his web site
- PHP now (Version 5)
- stands for "PHP: Hypertext Preprocessor"
- developed and used by a large community (> 2.5 million people)
- is a fully-fledged programming language but mostly used for server-side
scripting
- the most widely-used programming language on the web (for >40% of all
webapps):
Programming languages and scripting languages
- PHP is sometimes called a scripting language
- A scripting language is a special kind of programming language
- A program written in a scripting language is sometimes called a script
- So where's the difference?
- A script usually drives some other system, e.g.:
- A client-side script gets the browser to do things
- A server-side script gets the server to do things
- A shell script gets the operating system to do things
- Scripts usually do this by invoking pre-existing components (which may
not be written in the scripting language)
- Scripts are often interpreted (e.g. by a module in the browser,
server or operating system)
- Eek! Does this matter? No. (Not now.)
Static vs. dynamic web pages
Static web pages
- HTML is stored in files on the server
- A client makes a request; the server retrieves the relevant file from disk; it then
sends the content of the file to the client
- The only way to change the web page is to edit the file
Static vs. dynamic web pages
Dynamic web pages
- Programs (scripts) are stored on the server
- A client makes a request; the server retrieves the relevant program from disk;
the program is executed; and the output of the program is sent to the client
- The output may depend on the date, user input, contents of a database,…
greetings1.php
: our first PHP script
<?php
echo 'Hello world!';
?>
Notes on greetings1.php
- Filename extension is
.php
, not .html
- Embed the PHP code between
<?php
and ?>
- Alternatives (that we will not use):
<? ... ?>
<% ... %>
<script> ... </script>
- Use whitespace (spaces and new lines) for readable layout
Notes on greetings1.php
echo
is a PHP command
- It tells the computer to insert a string of characters into the HTML
- The
echo
command (and other PHP keywords) are case-insensitive:
echo, ECHO, Echo,...
- An instruction such as this:
echo 'Hello world!';
is called a statement
- Simple statements are terminated by a semicolon
- But
greetings1.php
is not producing a valid web page!
greetings2.php
: our second PHP script
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Our second PHP script</title>
</head>
<body>
<p>
<?php
echo 'Hello world!';
?>
</p>
</body>
</html>
Notes on greetings2.php
- What happen when the user generates an HTTP request to this file
(by typing the URL or by clicking on a link)?
- Because of the file extension (
.php
), the server passes the
request to the PHP processor
- The PHP processor works through the file line by line and handles each line
using either
- copy mode, or
- interpret mode (execute)
- The PHP processor's output is sent to the client browser
Notes on greetings2.php
Distinguish:
- The PHP script
- a mix of HTML and snippets of PHP
- since it's not just HTML, probably will not validate
- The output of the PHP processor:
- just HTML, no remaining PHP
- since it's just HTML, this can validate
greetings3.php
: our third script, which outputs tags
…
<body>
<p>
<?php
echo '<strong>Hello world!</strong>';
?>
</p>
</body>
Our fourth script (news.php
): something less pointless
…
<body>
<header>
<?php
echo date('D F j Y, h:i:s a');
?>
</header>
…
<body>
Notes on news.php
- This inserts the current date/time into the HTML
- The characters inside the quotes say how to format it, e.g.
- Class exercise: Suppose
news.php
is on our server in Cork. Someone
in Australia requests it. Whose date/time do they see?