Computer Science
Server-Side Scripting

Server & Client-Side Scripting

When your browser requests a web page from a web server using HTTP, the server responds by sending a copy of the file to your browser. This file is interpreted and rendered by the browser.

The instructions used to render the page are all executed on the client machine. HTML, CSS and Javascript instructions contained within the text file describe the content and formatting of the web page that the browser will display. This is called client-side scripting.

Server-side scripting is where the script is interpreted by software running on the server. The script will usually alter the content of the requested web page based on selections made by the user, possibly even using a server-based store of data - a database, XML or text file containing data that is queried before display on the web page.

When browsing, you may come across the acronym CGI which stands for Common Gateway Interface. CGI is the interface between the HTTP server and the extension that will execute the script. Server extensions are programs that generate the web page code from the scripts that are sent to the server. CGI, then, in principle is concerned with a request from a browser and the response from the web server extension.

Technologies

ASP

Active Server Pages are web pages contain VBScript. It requires a Windows platform on the server and that pages containing ASP have the extension .asp.

PHP

PHP stands for Hypertext Preprocessor. It is open source and widely used on the WWW. There are many extensions to PHP itself making it possible to connect PHP scripts to a range of tools like databases as well as construct images and PDF files from server-side scripts.

PHP is often used in conjunction with MySQL, an open source online database management system.

Perl

Perl can be used in much the same way as PHP. It was initially designed as a UNIX scripting language and is still widely used today.

Others

It is also possible with CGI to use compiled files, written in high-level languages like Delphi.

Some PHP Examples

In order to play around with PHP and MySQL, you will need to install a WAMP server. Links to download locations are provided in the SQL section of the site.

Simple Test

First we will start by seeing if you have managed to get a web server and PHP installed on your machine. Once you have done this, copy the following code into a text editor,

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
echo "<h1>Hello World</h1>";
?>
</body>
</html>

The bold section is the PHP. Notice that we use the PHP tags to escape from HTML. The remainder of the file (that not enclosed in the PHP tags) is plain old HTML. This file needs to be saved in your WWW directory - call it test.php. If your web server is running, you should be able to see this file in your browser by entering the URL, http://127.0.0.1/test.php in the address bar.

Now look at the source code that was delivered to the browser - click on View, Source Code. You should see that there is no trace of the PHP, all that you can see is the web page that was delivered once the PHP statement that we wrote was interpreted. This is handy too - we hide the complexity of our work from the user and deliver what we need to them - they don't need to know how it was done.

Using A GET Request

There are 2 main ways to pass information from the browser (and therefore, more often than not, from the user) to the server extension. The first of these methods is by using a GET request. A GET request is passed in the URL - it is a query string which appears after a question mark, directly after the URL. The URL of this web page contains a query string. Notice that the same script is used to produce all of the pages in this section of the site. The query string appended to the URL determines which page is sent to the browser.

The following script should be copied to a new file in your text editor and saved with the name get.php.

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
$thename= $_GET['name'];
echo "<h1>Hello ".$thename."</h1>";
?>
</body>
</html>

Load up the page in your browser - remember to type in the URL so that the request is made to the web server. Add ?name=Bob to the URL and press enter. The name, Bob should be displayed on the web page. Change the name in the query string, enter something rude and generally enjoy the experience.

Notice how variables are used in PHP. They must start with a dollar and are case sensitive. They are not typed. This means that, unlike in C#, you don't decide on a data type for your variables.

The following example uses a simple HTML form to allow the request to be made from a web page. The isset function is a very handy function that checks to see if a GET variable exists. The response to the request is delivered using the same script. This allows you to create dynamic web pages. Again - the HTML that goes to the browser is all that is needed.

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
if (isset($_GET['name']))
{
$thename= $_GET['name'];
echo "<h1>Hello ".$thename."</h1>";
}
else
{
echo "<form action='get.php' method='GET'>
Enter your name: <input type='text' name='name' size='50'><br>
<input type='reset' value='Clear'>&nbsp;<input type='submit' value='Submit'>
</form>";
}
?>
</body>
</html>

Look carefully at the action attribute of the form tag. This is a path to the script that handles the request. The method attribute determines how the form data should be passed to the server. The GET method puts the information into the URL.

Adapt this script to display two pieces of information, both entered by the user, in the resulting web page.

Using A POST Request

The GET request can make URLs quite long if you want to pass quite a bit of information. The POST method buries the data in the body of the request message. It does not appear in the URL and no information is visible to the user. Apart from the security issues involved, it is also a more user-friendly way of presenting the page to the user.

The following script should be copied to a new file in your text editor and saved with the name post.php. This script performs the same task as the previous example except that the POST method is used. The user data is passed to the server in the body of the message. The result is the same.

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
if (isset($_POST['name']))
{
$thename= $_POST['name'];
echo "<h1>Hello ".$thename."</h1>";
}
else
{
echo "<form action='post.php' method='POST'>
Enter your name: <input type='text' name='name' size='50'><br>
<input type='reset' value='Clear'>&nbsp;<input type='submit' value='Submit'>
</form>";
}
?>
</body>
</html>

MySQL Example

When you combine a script with an online DBMS you get quite a powerful set of technologies. Go to the SQL section of the site and create the seeds database, seeds table and import the sample data (SQL Section).

In order to extract information from the database, we need to do a little more than we had to do for the simple GET and POST requests.

The first step is to make a connection to the database, then pass a query to the DBMS. The results, if there are any, can be used in the web page response. Then the connection is closed. Notice that the connection statement requires a host, user and password. If you didn't set a password, then you should leave these statements as they are shown - otherwise replace them with your username and password.

The following script produces an HTML table of the contents of the tblseeds table in the database, seeds. <?php
$h = "localhost";
$u = "root";
$p = "";
$connection = mysql_connect($h,$u,$p);
if (!$connection)
{
die("Could not connect to the database");
}
mysql_select_db("seeds");
$query = "SELECT * FROM tblseeds";
$result = mysql_query($query);
echo "<table>
<tr>
<th>Name</th><th>Type</th><th>Seed</th><th>Price</th>
</tr>";
while ($row = mysql_fetch_array($result))
{
extract($row);
echo "<tr><td>".$name."</td><td>".$type."</td><td>".$seed."</td><td>".$price."</td> </tr>";
}
echo "</tr>\n</table>";
mysql_close($connection);
?>

The die() statement is a lovely addition in PHP - you don't need it but it feels good. Normally passwords would be placed in files outside of the WWW directory on a web server and permissions for that file restricted so that it can only be accessed by a script on the local machine and not from another source. This used a select query and results were returned.

Try Some More

PHP is quite flexible and easy to use. It has a modular structure and, using the manual, you can easily find functions grouped by purpose. Have a look online to see what kinds of projects are suited to PHP. Then dive in, use the manual when you get stuck and you can't go too wrong.