CSCI399
Autumn Session, 2011

Exercise 3

This exercise covers some preliminary work that you should attempt before you start Assignment 3. These exercises involve creating PHP files (Hello.php etc) in your public_html directory. You will need to allow read access by others. You can test your scripts by aiming your browser at http://localhost/~username/Hello.php etc.

You can start this exercise using a simple text editor to create your HTML pages and PHP scripts, and the sqlplus utility to access your database.  The later parts of the exercise use NetBeans to both create PHP code and to access the Oracle database.  NetBeans has specialized language aware editors for PHP, Javascripts, and CSS stylesheets.  You will find that you are more productive when using such tools rather than working at the command line and with ordinary text editors.

  1. Try the usual "Hello World" program - PHP style:
 
 
<?php
     echo "Hello -using php;";
     phpinfo();
?>
 
  1. View the PHP documentation - you should find the HTML docs in /usr/local/docs/php (there is an index.html file that serves as root for the documentation). Check on the functions for setting cookies etc.
  2. Test connection to the database (you should have created and populated a “soccerleague” table in an earlier exercise):
 
 
<?php
   $conn = oci_connect("homer","doh","//wraith.cs.uow.edu.au:1521/csci");
  
  $stmt = oci_parse($conn, "select * from soccerleague");
 
  oci_execute($stmt);
  print '<table border="1">';
  print '<tr><th>Team1</th><th>Team2</th><th>Score1</th><th>Score2</th></tr>';
  while($row = oci_fetch_array($stmt, OCI_ASSOC)) {
   print '<tr>';
     print  "<td>" . $row['TEAM1'] . "</td>";
     print  "<td>" . $row['TEAM2'] . "</td>";
     print  "<td>" . $row['SCORE1'] . "</td>";
     print  "<td>" . $row['SCORE2'] . "</td>";
   print '</tr>';
 }
?>
 
 

(Note Oracle requires column names in upper case.  Naturally, you should use your Oracle username and password; homer’s accounts on Ubuntu and the Oracle system have been suspended as a punishment for his eating in the laboratories.)

  1. Test graphical outputs:
 
 
<?php
  header("Content-type: image/png");
  $fontdir= "/usr/bin/jdk1.6.0_18/jre/lib/fonts/";
  $fontname = "LucidaBrightItalic.ttf";                
  $fd = $fontdir . $fontname; 
  $width = 200;$height = 200;
  $image = imagecreate($width, $height);
  $white = imagecolorallocate($image, 255, 255, 255);
  $green = imagecolorallocate($image, 0, 240, 20);
  $ptsize = 24;$x = 20;$y = 100;
  imagettftext($image, $ptsize, 0, 20, 110, $green, $fd, "Hello world");
  imagepng($image);
  imagedestroy($image);
?> 
 

(You need "True Type Fonts" - Java includes a few of these so if you cannot find any others you can usually use those that come with your Java installation.  The example code assumes that the Java is version 1.6.0_18; actually, we may have a more recent Java installed, you should look in /usr/local and find the current Java and adjust the path in $fontdir.  You can find more by going to a Windows system and searching in various system folders for truetype font files that you can copy.)

  1. Remove any existing NetBeans related folders that you may have in your home directory.
    You may have used NetBeans in CSCI110, CSCI213, or CSCI222.  You will have left over “hidden” directories as well as some projects in a NetBeansProjects folder.  The projects don’t matter (except that they may use up some of your file quota) so you can leave them.  It is NetBeans’ working directories that can cause problems.
    These are .netbeans, .netbeans-derby, and .netbeans-registration.
    It is best to delete these folders and all their contents.
    Sometimes left over configuration files in these hidden directories can cause lots of problems when you work with a newer version.
  2. Creating a NetBeans PHP Project.
    Start NetBeans from the Applications/Programming menu.
    The NetBeans installation supports programming in Java, PHP, C++, Ruby, etc.  Consequently, it has lots of code modules.  It only “activates” modules when needed.  So, the first time that you use NetBeans for PHP there will be a slight delay while the PHP modules are linked into the application.


Start by creating a new project – type PHP application.






For these exercises, it is simplest if you use your public_html directory as the “sources folder” (you can have more elaborate arrangements – including an option for deploying onto a remote server via ftp).  The project name becomes a directory name – all files for the project are kept in that directory.  This helps prevent your public_html directory from becoming too cluttered.  The project name is part of the URL that you will use when accessing a NetBeans built project.




Screenshot-2

NetBeans used always to create a dummy PHP web page – “index.php”.

Screenshot-3

This default “index.php” wasn’t useful for most examples so it was best to delete it.  The current NetBeans may not generate this dummy file

  1. Editing a HTML file.

    This example is intended to be an updated version of Rasmus’s example with a form for entering name and age, and a script for processing these data.  First, the form page can be created.
    Pick the project and select add new HTML file


    An empty HTML document is created:



    Things like the title, and content text like a marked up header (e.g. <h1>Rasmus’s example for PHP</h1>) must be edited in; NetBeans helps with things like getting markup tags correctly closed.

    NetBeans has a simple editor for plain HTML pages.  (It isn’t comparable to a Microsoft Visual Studio HTML editor which works in graphic mode; it shows only the HTML text, but there is a tools palette that provides some assistance when adding tables, forms, links, etc.)  The HTML tools palette isn’t displayed by default – use the Windows/Palette menu selection to get the palette displayed.



    A “form” can be created by dragging one in from the tools palette; this results in a dialog where form properties (like method, url of target etc) get entered.


    It is easy to link the Action (target) to an existing PHP file (found by browsing); but here the form is being created before the processing script so the URL was entered manually.  The script is to be call RasmuScript.php.

    Things like Buttons, Text Inputs etc can now be created within the form by dragging from the tools palette.  Each such operation results in the display of a dialog where one defines attributes of the form element.



It is possible to check the appearance of a partly built HTML page by selecting the file in the Projects pane, and right-click selecting “View”




  1. Aside: prettying up the HTML page with CSS stylesheets and Javascript
    This example is too small to justify the use of stylesheets or client side Javascript; but this is the point in the exercise where it is appropriate to illustrate how these components would be handled in a more significant example.

    A stylesheet, e.g. mystyle.css, can be created as a new file within the project (select the project, then right-click add new file, select CSS stylesheet – if the CSS option doesn’t appear automatically, try under “other”).  NetBeans’s stylesheet editor allows you to add new paragraph types etc:
    °
    In the example shown above, I change the default styles for an H2 header, and for dt and dd elements in a definition list.

    Of course, each HTML page that uses a stylesheet must contain a link to the stylesheet – this goes in the header section of the HTML page:

<head><title></title>

  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 <LINK href="./mystyles.css" rel="stylesheet" type="text/css" >

</head>

      Javascript function definitions can be inserted into the HEAD section of a HTML page:


      If the Javascript is at all involved, it is best defined in a separate file (there is an option under add new file to pick a Javascript file) that gets included in the HTML page.

  1. Continuing with the Rasmus example, we now need to define a PHP processing page.  Select the project, then add a new PHP web page file:



    (A PHP Web page is appropriate for examples where you want a fragment of PHP embedded in some HTML; a PHP script page is more appropriate when you have a reasonably complex script.)

    NetBeans provides a dummy page, just like the original index.php page initially created for the new project.

    Add PHP script code to process the input data (it has to be a little more complex than Rasmus’s original example code because data must be taken from global arrays instead of auto-defined variables).



    It is ready to run.  Aim your browser at http://localhost/~youruserid/RasmusDemo/RasmusForm.html



    The script should run:



  2. Connecting to and using the Oracle database:

    You should already have created some Oracle data tables as part of an earlier exercise or assignment.

    The NetBeans database connection is probably an easier way of manipulating data tables than sqlplus; so it is time to learn how to use this component.

    Check that you can connect to the Oracle database:
    In the NetBeans window, there is a panel with Projects/Files/Services - select Services.
    Within "Services", select database and by right-click pick "Add driver". (You have to “Add driver” and create a connection the first time that you want to use Oracle.  The details get recorded in those .netbeans directories.  The next time you use NetBeans, the driver set up etc should be remembered.)



    The full path to the Oracle driver must be supplied - /usr/local/instantclient_10_2/ojdbc14.jar. |



  3. After adding the driver, create a new connection using the driver. (Open the drivers in the tree-view, select the new Oracle entry and right-click select new connection.
    You will need to fill in configuration details:


    Our Oracle runs on the “wraith” server; it uses Oracle’s standard port 1521; the service id is CSCI.
    Supply your name and password – it’s probably simplest if you allow NetBeans to remember these values.


    The connection should open, and a new entry will appear in the list of database connections. Open its tree view; it will show all the schema in the CSCI service.  Your schema, the one with your tables should be highlighted (hopefully you can’t explore anybody else’s schema).

  1.  Select your own schema and open its tree view.  It will show subviews for Tables etc.
    Open the Table subview - your tables should be listed. Select a table and right-click select "View Data". An SQL query will be run and the results displayed.

    View table


    When satisfied that the database connections work, close the connection.
    These connections are useful for checking out SQL statements (you can enter your own SQL and test run it) and for viewing the data to see what your applications should return.

    You can create tables by entering a “Create table” SQL command.  (It is generally wiser to create text files with SQL commands to create and populate tables, and use these in association with sqlplus.  This makes it easier to drop and recreate a table in some standard state – useful when testing code.)


  2. PHP code working with database:
    Create a new PHP project (“DBTest”) and edit the default “index.php” page to have some code that uses PHP and its Oracle OCI library to access the same table:



    Try running – aim browser at http://localhost:8080/DBTest - oops, just get a table header and no data!

    What went wrong?

    Well, if something fails, it would be best to try looking at the Apache logs.  These will be in /var/log/apache2 (unfortunately, it’s likely that the Ubuntu system may have had to be configured without your having access to this directory.


    The “errors.log” shows a report of an Oracle error – logon denied.

    Yes, it doesn’t work if you try to use Homer’s Oracle account.  Edit the code to use your name and password.



When you have completed those exercises you can start on Assignment 3.