CPSC 120: Principles of Computer Science
Fall 2002

Lab 7: A Simple Weblog with PHP



WE HAVE BEEN DISCUSSING the xTurtle programming language in class. This language illustrates many aspects of programming, but it is not a "real" programming language in the sense that people use it for serious purposes. Real programming languages include C++, Java, Visual Basic, FORTRAN, Lisp, and many more. PHP is a language that is used for writing active web pages. An active web page does not just display some fixed content. Its content is generated by a program. The content is often constructed as a response to data entered by the user in a form on another Web page. In many cases, the content depends on data that has been stored in a database. You will see an application of this sort in this lab. You won't actually learn much about PHP programming, but you will get a chance to see how it is used.

Exercise: The only exercise for this lab is to create a WebLog, as described on the rest of this page. You must do this with a file named slog.php in your www directory. Try to make the Web page look attractive. Choose a nice format for the log itself. Try to make your Web log entries interesting for other people. Choose some interesting topic for the log, if you can. Add some images or extra text to the page. Change the title, headline, and colors. I will check your WebLog some time after next Friday's class. If you are working with another person on this lab, please let me know where to look for the web log.


A WebLog is simply a web page that displays entries (usually a couple paragraphs of text) submitted by one or more people. Someone might use a personal WebLog as a kind of public diary. Or they might post their comments on current events. An organization might use it to keep people informed about upcoming events. An athletic team might post comments about their games for their fans. WebLogs have become popular recently, and have come to be known as "blogs" (short for WebLogs). People who write WebLogs are known as "bloggers". The Web site http://www.blogger.com/ lets anyone publish a basic web log for free, and makes more advanced features available for a fee. You can read more about blogs and about blogger at their "about" page, http://www.blogger.com/about.pyra.

For this lab, I have written a very simple WebLog facility. Since it's so simple, I call it SLOG (short for "Simple WebLog"). This facility will allow you to post messages to your own web log. To access this web log, you need a user name and password. The web log uses the same user name and password that you were assigned for logging into the Linux system. To add an entry to your log, go to the page

http://math.hws.edu/eck/cs120/slog/edit.php

You will have to log in. When you do, you will see a page where you can make an entry to your web log. You should add your first entry now, to see how it works. (Don't worry too much about what it says. You can change or delete it later.) Enter your information, and add a headline for the entry. When done, click the button labeled "Click Here to Preview," and you will see your entry as it might appear on a web page. If you are satisfied, click on "Click Here to Accept". (If you are not satisfied, you can use your browser's Back button to return to the edit page.) If you accept the entry, it will be added to your log. There will be a button labeled "Click here to return to the SLOGger edit page." You can do this to go back and add more entries for your web log. You will also see that it's possible to select previous entries and edit or delete them. You will use the SLOGger edit page whenever you want to work on the content of your web log. Add a second entry now, so that you will have at least two entries in your log.

You can include simple HTML commands in your entry. For example, use <p> to begin a new paragraph or <br> to end a line. To put something in italics, enclose it between <i> and </i>. You can even include links and images.


The purpose of the lab, though, is to have some way of displaying your web log on the Web. For this, you can work under either Linux or under Windows. You will have to copy some files into your www directory. The files are named viewslog.php and slog.php. You can find them under Linux in the directory /home/cs120/Lab7. Under Windows, you can find them in the cpsc120 folder on the "N" drive. You need the file named viewslog.php, but you don't have to change it. To do the lab, you have to edit slog.php. So, copy the files into your www directory and open slog.php in an editor.


PHP is used to write active Web pages. PHP is not available on all Web servers, but it can be used on the server that contains your Web pages. A PHP file must have a name that ends with ".php". This is what tells the Web server to treat it as a PHP file. A PHP file can contain HTML code, just like an ordinary page. However, it can also contain programming code written in PHP. This code must always begin with <?php and it must end with ?>. Everything between <?php and ?> is considered to be part of a PHP program, and the computer tries to execute it. The job of the program is to write all or part of the content of the Web page.

The purpose of slog.php is to display the contents of your web log. This file already contains the PHP program

                       <?php 

                          $sloguser = "myusername";
                          $password = "mypassword";
                          include("viewslog.php");

                       ?>

The first two lines specify the user name and password for the SLOG system. You should change mysername and mypassword to your own Linux user name and password. As soon as you do this, your web log will be visible on the Web at an address of the form

http://math.hws.edu/~username/slog.php

with username replaced by your own user name. Try it. You should see the web log entries that you wrote earlier. However, they are in a rather simple format. You can change the format by adding certain commands to the PHP program. Note that you can also add regular HTML code to the web page, outside the PHP program. For example, you can add some explanatory text before the log entries.

The line $sloguser = "myusername"; is an example of a PHP assignment statement. Assignments in PHP use the "=" operator, not ":=". Variable names, such as $sloguser, must begin with a $. Every statement must end with a semicolon. These are details of syntax. Although they differ from the syntax of xTurtle, the idea of an assignment statement is exactly the same. The line include("viewslog.php") executes the contents of the file named viewslog.php (sort of like a subroutine). This is where the web log is actually output to the Web page. (I put all the details in another file because I don't want you to have to look at them. However, you are certainly welcome to take a look if you want.) The program in the viewslog.php file uses the values from the two assignment statements. You can customize the appearance of your web log by adding other assignment statements before viewslog.php is read. For example, if you change the program to:

                       <?php 

                          $sloguser = "myusername";
                          $password = "mypassword";
                          
                          $style = "tables";
                          $headline_background = "#DDDDFF";
                          $text_background = "#FFDDDD";
                          
                          include("viewslog.php");

                       ?>

then each web log entry will be shown as a separate table, with a light blue background behind the headline and a light yellow background behind the text of the entry.

The assignment for this lab is to customize the appearance of your web log, to customize the HTML code for the rest of the page, and to put several entries on some interesting topic into the log. Here is a list of all the assignment statements that you can use to customize the web log. The value shown here is the default value (that is, the value that will be used if you do not specify another value):

      $style = "plain";
             Possible Values: "table", "tables", "rows", "plain".
             This controls the basic style of the web log.
             The plain style just outputs the log entries as
             paragraphs.  The "table" style puts them in one
             big table, while "tables" puts each entry in a
             separate table.  Is you use "rows", you should
             put the log into a table that you make yourself.
             The log entries are put into rows of a table,
             without the <table> or </table> tags.

      $limit = "0";
             Possible Values: an integer >= 0.
             This will limit the number of entries displayed.
             If you have a lot of entries and only want to
             show the most recent 10 entries, set this value
             to "10".  The default value "0" means that all
             entries will be shown, no matter how many there are.

      $show_headline = "yes";
             Possible Values: "yes", "no".
             If you set this to "no" then the headlines of
             your entries will not be displayed.

      $show_time = "yes";
             Possible Values: "yes", "no".
             If you set this to "no", then the time at which
             an entry was posted will not be displayed.  By,
             default it is displayed in small italic type after
             the entry.

      $text_color = "";
             Possible Values: "" or any HTML color specification.
             Specifies a color for the text of the entries.
             You can use a color name such as "red" or a hexadecimal
             color specification such as "#EEFFEE".  The special
             value "" will use use the same text color as the rest
             of the page.  See 
                   http://math.hws.edu/eck/cs120/f02/lab1/colors.html
             for a table of colors.

      $headline_color = $text_color;
             Possible Values: "" or any HTML color specification.
             Specifies a color for the headlines.  The special
             value "" will use use the same text color as the rest
             of the page.

      $center_headline = "yes";
             Possible Values: "yes", "no".
             With the default value, "yes", the headline is displayed
             centered over the entry.  If you prefer to have it shoved
             to the left, change the value to "no".


  These are used only if the style is "table", "tables", or "rows":

      $text_background = "";
             Possible Values: "" or any HTML color specification.
             Sets the background color for a table cell that contains
             an entry.  The default value, "", just takes the background
             that is used on the rest of the web page.

      $headline_background = $text_background;
             Possible Values: "" or any HTML color specification.
             Sets the background color for a table cell that contains
             a headline.  The default value, "", just takes the background
             that is used on the rest of the web page.


  These are used only if the style is "table" or "tables":

      $table_border = "2";
             Possible Values: a non-negative integer.
             The value of the "border" attribute in the <table> tag.

      $table_padding = "5";
             Possible Values: a non-negative integer.
             The value of the "cellpadding" attribute in the <table> tag.

      $table_width = "500";
             Possible Values A percentage or a positive integer.
             The value of the "width" attribute in the <table> tag.

      $center_table = "no";
             Possible Values: "yes", "no".
             If this is set to "yes" then tables will be centered by setting
             the value of the "align" attribute in the <table> tag
             to "center".


  This is used only if the style is "plain":

      $add_lines = "yes";
             Possible Values: "yes", "no".
             With the default value, a horizontal line is added after each
             web log entry. Change this to "no" to get rid of these lines.

--David Eck (eck@hws.edu)