• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
Stored as "php.html.doc"College of St. CatherineLIS 753
Lab Exercise -- PHP
Instructions
 Now it gets interesting. As we’ve talked about in class, XHTML is good for presentinginformation, but in the end it is only a flat markup language. You literally use XHTML to“markup” the text and graphics of your pages to give the browser instructions on how todisplay the text and graphics on that page. XHTML pages by themselves cannot acceptany input from the user, and they can’t dynamically change based on outside information.Think about this: although good content is the true key to a good website, what web sitesdo you go back to again and again? Times up! I’ll tell you: you go back to the web sitesthat have good content and that you can interact with. This includes Amazon.com,Google.com, your favorite library catalog, the list goes on and on. What do you think?Should we try to create some interactive pages of our own?In order to create an interactive web page, you’ve got to know more than just XHTML.You’ve got to know a programming language. Lucky for you, St. Kate’s has put the most popular server side programming language onto its web server: PHP. As I’ve alreadynoted in the lecture, PHP was invented in the early 90’s by Rasmus Lerdorf, and all hewanted to do was count the number of people viewing his resume. Today, PHP is now afull-fledged programming language on par with heavy weights like ASP and Java. Andthanks to its popularity (and the fact that it is free), when you get your first job in the“real world” most likely the library you work at is also going to be using PHP. Learninga little about it now will definitely give you a leg up.And although PHP is great for helping people create interactive web pages, PHP can alsodo a lot of stuff behind the scenes to make your life as a fledgling webmaster a whole loteasier. For example, by using PHP includes we can create a standard “header” and“footer” that we can use on every page that we create from now on. Imagine never having to type all that XHTML crapola on your pages again, except for one page that yourefer to on all of the pages on your site. Think of PHP includes as CSS on steroids.For our first foray into PHP we are going to learn how to create these wonderful includesand we are going to create a mail form. Some of you may be thinking this is pretty lame, but hey, we gotta start somewhere. Some of you may also be thinking that this is a lotlike the Forms lab. The big difference between this and the Forms lab is that in this labyou will be creating both the form and the email script the form points to in the action.So let’s get started!
 
Step 1: Open Notepad (or Simpletext) and create a New document.Step 2: Type in the following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252" /><title><?php print "$title" ?></title></head><body bgcolor="#ffffff"><h2><?php print "$title"; ?></h2>
Step 3: Save this document as header.php and FTP it to your directory on paradox.stkate.edu
Let’s talk about this file a little bit. First of all you should notice that as far as XHTMLdocuments go, it isn’t finished. It doesn’t include the closing </body> or </html> tags.Good catch. Luckily, this file will never be used by itself. It will always be used inconjunction with both a main body text type file, and a file called “footer.php.”“footer.php” is where we will complete our XTHML. Are you with me so far? Let’s takea look at a little more then. You should also note that we saved this file as “.php”. This isvery important. This tells the server that it should be aware that some PHP code may be present in this file. If we had saved this file with the .html extension, the server wouldignore any PHP code we included. A file that is saved as .php doesn’t have to have PHPcode, but if you are including PHP code the file has to be saved as .php. Does this makesense?You’ll also note that header.php contains some funky coding that we’ve never seen before, most of which starts with “<?php”. This is a PHP code block. PHP code blocksalways start with “<?php” and end with “?>”. What this bit of code does is tells theserver to parse this section of code as PHP while leaving the rest of the file alone, or assimple XHTML. Now check out the variable called $title. If you’ll recall from thelecture, a variable is a bit of code that acts as a container for a value we wish to use morethan once. So, what is the value of this variable? Ah … good question. We haven’tspecified what the value is yet. We will do that in another file down the road. But youmay also want to note that once we do specify that value it will be both the title of theXHTML file, and it will appear at the top of the page in between some <h2> tags sincewe are printing it out in these areas of our code. Let’s move on to the next file.
Step 4: Open Notepad (or Simpletext) and create a New document. Type inthe following code:
<br /><br /><hr noshade="noshade" />
 
URL: http://www.stkate.edu<?php print $PHP_SELF?><br /><?php $last_revised = date("F d, Y", getlastmod());print "Last revised: $last_revised<br />"; ?></body></html>
Step 5: Save this file as footer.php and FTP it to paradox.stkate.edu.
This is the end of the XHTML we started in header.php. Again, this file will never beused alone. It will always be used in conjunction with at least one other file. There arealso two other bits of PHP code in the file. First of all, we have the environment variable$PHP_SELF. An environment variable is a variable that you don’t specify yourself. Itcomes automatically with the server running PHP. There are a bunch of them, but for our  purposes we will just deal with this one. $PHP_SELF is a variable that holds the value of the name of the file itself. So, by printing it out in this way, we can print out the actualURL of the file dynamically. This means, if all goes as planned, we shouldn’t have tokeep manually writing out the file name on this line. PHP will do it for us every time weuse footer.php. Slick, heh?Secondly, the footer.php file contains some “last revised” information which lets thereader know when was the last time this page was updated. Again, why should we haveto manually type this information in when the server will do it for us? So, first of all wecreate a variable named $last_revised. And we assign that variable the value of 
date("Fd, Y", getlastmod()).
I know what you are thinking: huh? What this is is thedate() function of PHP. PHP comes with a whole bunch of built in functions that extendthe power of the programming language. In this case, this function let’s us work withdates. Specifically, it prints out the date in “F, d, Y” format. In the date() function, the“F” stands for the month, the “d” stands for the day, and the “Y” stands for the year.Check outhttp://us2.php.net/manual/en/function.date.phpfor more date codes that youcan try. We also specify another function inside the date function: getlasmod(); thisfunction obviously gets the last modified date from the server for the file it is used on.Used with the date function and our fancy coding we print it out for the user to see. Atleast that is the goal. Let’s move on.So, we’ve got the header and footer for our site. Now all we need is a file to attach these puppies to. So, let’s create a file, but this won’t be any ordinary file. It will be one onwhich the user can finally interact with our site. It will be a mail form!
Step 6: Open Notepad (or Simpletext) and create a New document. Type inthe following code:
<?php$title = “Mail form”;include(“header.php”);?><p>Thanks for taking the time to let me know what you think of myhumble site. Please use the form below to submit your comments tome:</p>
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...