Web Design Fall 2008
Scripts

Course Syllabus

Intro

Design

Slicing

CSS

Template

Content

Scripts

Meta-Tags

Uploading

Website Hosting

Animated Gifs

Requested Elements

Further Readings

Interesting Designs

What is PHP

PHP stands for Hypertext Preprocessor. It is a coding language that allows a web designer to specify particular actions to be preformed on the server prior to sending a requested page over the internet to an end user. PHP outputs the final code in HTML format for the viewer to see. This means that any page can be converted to a PHP page by simply changing the file extension on that page to .php in stead of .html. PHP has unlimited uses including:

  • receiving forms and e-mail submissions
  • inserting user/visit specific information such as time, user IP address
  • combining separately designed pages together such as a navigation page.
  • communicating with a database and relaying information within that database.
  • updating database content for content management systems
  • and many many more....

How to implement PHP

PHP, as state above is a server side scripting language. A server knows that a file should be checked for php code by the file extension. So to implement a php script the first step is to make sure the file extension is set to .php. Once a file has the extension .php on it the server will know to check that document for php scripts to execute before sending the page to a viewer.

The code that defines the start of php scripts to be run on a server is <? and the inverse of this is used to define the end of a php script ?>. These opening and closing statements can be put into your .php document anywhere where you want to have a php script. However, anything inside of the <? ?> will be executed as php and therefore must be in php format to avoid error messages.

PHP Demonstrations

Showing the current date: this demonstration will show the current date on a webpage

  • Start with a new .php document by going to file -->new then save the file
  • Switch to the code view for this new file
  • in between the body tags of the file insert the code
    • <? echo date('Y-m-d'); ?>
  • Save the file and upload it to the server.
  • Browse to the file on your server using firefox and the result should look like this
    • 2025-01-22
  • If your date is showing up correctly, you have successfully implemented your first php script. Congratulations.
  • this code is operating as follows
    • <? is telling the server to start executing a php script
    • echo is a php function that tells the server to return the following value to the end user
    • date() is a function that gathers information about the current time
    • 'Y-m-d' tells the date function how to format the information.
    • ; tells the server that it is at the end of that echo operation and causes the script to be preformed
    • ?> tells the server that this is the end of the php script and to continue sending the rest of the page to the viewer.
  • Try using the echo function to display some text to go along with the date
    • <? echo "Today's date is: "; echo date('m-d-y'); ?>
  • The result should look like this
    • Todays date is: 01-22-25
  • Look at the code of your page by hitting view source in firefox. notice that there is no reminisce of the php code.

Using the include function: this demonstration will show you how to use the include function to combine 2 pages in html format into 1 for the user to see.

  • start with a new blank php document and save the file to your site root folder
  • switch to the code view for the file and insert the following script inside your body tags
    • <? include "nav.html"; ?>
  • create another new document this time of type .html
    • switch to the code view of this document and delete everything
    • switch back to the design view of the document and insert some text
    • save the file to your site root folder and call it nav.html
  • upload both files to the server.
  • in firefox browse to the php page you created.
  • notice that the text from the nav.html document has been incorporated into your .php document. this code is doing the following
    • <? opens php script
    • include is a function that tells the server to go find a file and include whatever content there is in that file into the current page
    • "nav.html" specifies the page that should be included this can be set to any file you choose
    • ; ends the include script and tells the server to execute the line of code
    • ?> ends the php script

This function is very useful for the navigation area of your website. This is especially true if you anticipate the need to ad additional pages later on. If you place this script on each on of your pages in place of your navigation buttons, and links you can then modify the navigation element across your entire site by simply modifying the nav.html file which is where you put all your buttons and links.

Using PHP for user submitted forms

A form on a website consists of 2 parts. First is the form its self which the user will fill out and then click a submit button. The second part is a php script that gathers the data the user has submitted and then performs an action with that data such as e-mailing it back to the server admin.

Feedback form and submit button: This link is to a php file within a zip file that contains all the scripting necessary to have a feedback form on a website. This is the most simple execution of this functionality and doesn't include any safeguards or required fields. This file is commented to explain the different actions being preformed