Agenda

Server Development Environment

A common practice among developers is to create a Server/Client environment on a local network, closed to the world wide web. This provides a private, safe place to test and debug sites that use server side scripting. There are several HTTP server programs available to choose from.

HTTP servers

Exercise: Install and run an HTTP server

Install a web server environment on your client machine.

  1. Download an open source server package for your operating system:
  2. Important: if using BCIT lab computers, install the server to the C: drive
  3. Install the downloaded package with default settings
  4. Start the http server
  5. Visit http://localhost in your web browser. Mac users visit visit http://localhost:8888

Troubleshooting

Unable to get yor server running? There may be a port conflict. The http protocol runs on port 80 by defualt. If your computer has another application already utilizing this port, your server will not run (For example Skype commonly uses the same port, and can cause conflicts with your Apache server).

You will need to make some changes to the Apache server configuration file:

Trouble In Older Windows OS

If your WAMP installation complains about missing DLLs when you try to run it, there may be a series of visual C++ packages that need to be installed before your server can run.

Server Structure

Server Root

Locate the www (Windows) or htdocs (Macintosh) folder in your server installation. This is known as the server root. Any documents that your server needs access to must be placed inside this folder or in subfolders. Documents not inside this folder cannot be retrieved by the server. The server root physical address is mapped to the http://localhost virtual address. When you visit localhost in a web browser, your server will display the contents of the server root.

HTTP Ports

Internet communications associate port numbers along with the protocol and host IP address. The default port for HTTP transactions is 80, and HTTPS uses 443. When the default port is used, it does not need to be explicitly stated in a URL. If an alternative port is used, it must be included after the hostname:

Ports are not shared, so if one application is using a port already (Eg Skype uses both ports 80 and 443) then other applications must use alternative ports.

Server Paths

Relative paths point to a file relative to the location of the current page. On an HTTP server, paths may instead be written relative to the server root.

Path Syntax Examples

Server Configuration

There are two ways to administer an Apache web server, either using the server configuration files or using a special text file named .htaccess

  1. Server 'config' Files

    There is a configuration file used for determining how the server behaves: http.conf. Access it via the WAMP icon on your taskbar

    If you have administrative access to the server, you can make changes directly to these config files. If you save these changes, then restart the server, the changes will take effect.

    Find and open the file httpd.conf in a text editor. Locate the following directives in the file:

    • Listen 80 the port your server is accepting requests on (default HTTP port is 80)
    • DocumentRoot defines your server root folder
  2. Managing The Server With .htaccess

    The .htaccess file is a text file used by the Apache HTTP server to configure basic behaviour without requiring server administrator privileges. Place this file in the server root for it to take effect

    The .htaccess file is very powerful, and especially useful when you do not have access to the server configuration file (httpd.conf). You can determine what is displayed when errors occur (Error 404, 500, etc), prevent hotlinking of your images, etc.

    Editing 'Invisible' Files On Mac

    Macintosh developers may need to enable the visibility of system files such as .htaccess. The following hotkey combination should toggle the visiblility of system files on MAC OS:
    First activate the Finder app then use CMD + Shift + .

.htaccess

The .htaccess file can configure server behavior. If you place an .htaccess in the server document root folder, it's directives will affect the entire site. If you place it in a subfolder, it will affect all files in that folder. More than one may be included in your website. Deeper nested .htaccess files can over ride higher level ones.

Using .htaccess

Server Side Scripting

Server side scripting provides the ability for the HTTP server to build dynamic responses to client requests.

Exercise: Run A PHP Script, Configure PHP

PHP Fundamentals

PHP is a powerful open source server side scripting language supported widely across many web hosting platforms.

Enable PHP With .html Files

By default, only files with .php will be parsed for PHP instructions. In COMP1950 you will be adding PHP to .html files you have already written, requiring more server configuration using the .htaccess file located in your server root.

Note: when you configure the server to parse .html files for PHP instructions, you could end up wasting server resources (eg: if a client requests an .html file that contains no PHP, the server will still need to parse it before response).

Include Files

Server side includes allows developers to reduce duplicate code across a multi-page site. Reduced duplicate code shortens development time and makes site maintenance more effective.

Split the code of your pages into separate files, based upon the role. Separate blocks of code that are shared by more than one page from code that is unique to a page. For example:

If the code for the heading is located in the file header.php, an .php page can import this code with the include() instruction:

<?php
	include("header.php");	
?>

Functions

A function performs a useful task for the developer, such as connecting to a database or reading/writing files. The PHP.net function reference provides documentation on the hundreds of useful built in functions available.

To use a function, name it and provide it any data it needs using a parameter list defined by round brackets ( ). Most functions, when complete, will return data to the instruction that invoked it. This returned data can be captured in a variable for later use.

<?php
	$resultOfFunction = functionName("parameter");
?>

The date() Function

If the PHP date/time is set correctly, the date() function can be used to determine date/time information.

IMPORTANT: Set the correct timezone for your PHP installation by changing the configuration file. Find the date.timezone declaration in the php.ini flie and make the following changes:
date.timezone = Canada/Pacific
Save these changes and restart the Apache server. If you dont have access to the php.ini file, you can set it in PHP code using the date_default_timezone_set() function.

The parameters passed to date() will determine how the date is formatted. View the date() format character chart to see how to customize the output. For example:

<?php
	//set correct timezone in PHP	
	//this will over-ride the timezone set in php.ini
	date_default_timezone_set("Canada/Pacific");
	
	//determine what day it is today
	$today = date("l");	

	//prints something like "Monday"
	echo "<p>$today</p>";
?>

The pathinfo() Function

The pathinfo() function returns information about a file path, and can be used to learn the name of the current file. For example:

<?php
	//determine the filename of the current page
	$filename =  pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
	
	//display the filename (without the extension)
	echo "<p>$filename</p>";
?>

Logic

Scripting logic allows for program flow control. PHP can choose to execute blocks of code based on various conditions defined by the developer using the if control structure.

If Statements

If statements ask a question that can be answered with true or false, known as a boolean condition. If the statement is true, the instructions associated with the if statement will run, if the statement is false, they will not run

Comparision Operators

Comparison operators are often used in conjunction with control structures such as 'if', 'for', 'while' etc

Code Example

if($userAge > 18){
	echo "You are an adult";
}else if($userAge == 18){
	echo "You recently became an adult!";	
}else if($userAge == 17){
	echo "You will soon be an adult.";
}else{
	echo "You are not an adult.";
}	

To Do

The session 9 quiz will be closed book, written.