Web Server Programming

This page is about server-side Web programming. Before you can do such programming your server needs to be set up. See Web Server Administration for details.

Server-side programs have two major components- a data engine (usually a relational database) and a scriptable Web server or one with a CGI interface.

=Databases= It's probably possible to build a web application without a database, but I wouldn't recommend it. The database is where your data lives, and data is the important part of your app. Using a database means you don't have to figure out how to structure your own data. MySQL is a relational database engine. Relational databases store data in tables, like so:

This probably looks familiar to you if you work with spreadsheets. Spreadsheets also use tabular data, and spreadsheets are ok, but database > spreadsheet. Let's look at this table. First off, you wouldn't have that first column in a spreadsheet (because the spreadsheet already provides it). BoxID is the "primary key" for this table. Every table must have a primary key. The primary key serves two important functions: What are relations between tables? We'll get to that in a bit. First, let's have a look at the RAMconfig column. Why are there numbers for RAMConfig? Those numbers don't really describe anything, do they?
 * It is a unique identifier for each record (row) in a table.
 * It serves as an endpoint to identify relations between tables.

Check this out:

So what, it's another table. This table lists a bunch of configurations of RAM. Big deal. But the other table has a column for configurations of RAM. If we had entered all that RAM data into the top table we would have had to repeat ourselves a bunch of times, since two machines each have RAMConfig number 2. Instead, we split off the RAM data into a separate table with its own data, and a primary key isomorphic to the RAMConfig column in the first table. Then we tell the database something like: ALTER TABLE `accounts` ADD CONSTRAINT `FK_myKey` FOREIGN KEY (`RAMconfig`) REFERENCES `RAMconfigs` (`configID`) ON DELETE CASCADE ON UPDATE CASCADE; That's basically what SQL (Structured Query Language) commands look like. That command is probably wrong, SQL is fairly complicated. OR you could just install phpMyAdmin and draw a little arrow going from RAMconfig in the Computers table to configID in the RAMconfigs table and hey presto, you've got a relation without typing all that complex stuff.

=Scripting=

Popular languages for Web programming include PHP, Perl, Python and Ruby. Any language can be used for Web programming, and many have. To avoid re-inventing wheels Web programmers frequently make use of *frameworks*, which are libraries of code available for re-use.

PHP is an old scripting language. The name kinda-sorta stands for Hypertext Preprocessor, which is PHP's main function. PHP has been around since the early 1990s (hence 'ancient', though Perl hackers still think of PHP as the 'new kid') and has since grown into a full-featured scripting / programming language. As of PHP 5 the language supports OOP, but that's a topic for a different class. PHP can be embedded in HTML files. The server can sometimes parse PHP in files with .html extension, but using .php extension is safer.

Here's a sample of some embedded PHP:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   <-- If we wanted to define some functions to use later we might do this. This isn't PHP yet.Note that this is an HTML comment, PHP comments show up later !> This is a sample page  //This is an opening PHP tag- it tells the server to send the following to the PHP interpreter. The server sends the output to the browser, not the code you see here. echo " You can use PHP to generate arbitrary HTML "; echo " getElementByID("php-generated")InnerHTML = "You can also use it to generate Javascript"; "; for(i=0, i>5, i++){ echo 'printing i numbers'; /*PHP treats single quotes differently from double quotes- one allows parsing within a string and one does not following is a different way to do this */ //echo "printing".i."numbers"; } 