PHP Setup and Introduction

PHP

PHP (a recursive acronym for "PHP: Hypertext Preprocessor") is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. PHP is focused on websites and web applications (server-side scripting), command line scripting and writing desktop (GUI) applications. Server-side scripting is the most traditional and main target field for PHP. In CPSC 304, we'll primarily concentrate on the aspects of PHP that focus on accessing a database.

Assuming that you already have a web browser, you'll need two things to make PHP work for you: (1) a web server and a (2) connected PHP parser (CGI or server module). You can access the PHP program output with a web browser, viewing the PHP page through the server. This tutorial will walk you through the steps of PHP set up on the ugrad.cs.ubc.ca machines using the configurations that we have prepared for you.

Setting up PHP

The undergraduate webserver has already run the Apache web server. So we can use it directly and don't need set up any web server anymore. What we need to set up now is:

Getting PHP accessing Oracle

First of all, test whether you can access Oracle:
Here is a file named test.txt for you to test if you can access to oracle. Rename it to have a ".php" extension like "test.php".
Replace "Username" and "Password" with your Oracle username and password. For example, $c=OCILogon("ora_a1a1", "a12345678", "ug")
Then run it by typing the following in a browser window: http://www.ugrad.cs.ubc.ca/~account/test.php

If you see a successfully connected message displayed in your browser window, then your PHP can connect to Oracle correctly.

Now you need to learn how to use PHP functions to manipulate your database. To help you with this, we have created some sample PHP files to help you learn through the examples and comments.

Click here to get the test script oracle-test.txt. To get it to work, you should put it in your web directory (default: $HOME/public_html), and rename it to have a ".php" extension. You also need to replace "Username" and "Password" with your Oracle username and password. Note that IF YOU HAVE A TABLE CALLED "tab1" IT WILL BE DESTROYED.

Debugging

Our PHP server does not report any errors and warnings because of the configuration in php.ini. You could see the config info by phpinfo().
<?php
   phpinfo();  // Print PHP version and config info
?>
We do not have permission to modify the php.ini.

Here is one possible solution to report php syntax errors and warning. For example, suppose that you want to check the hello.php and see if it is right. (spoiler: it's not)

<?php
   echo "Hello world foo"
s
?>
You could write the php file, for example called wrapper.php
<?php
error_reporting(-1);
ini_set('display_errors',1);
include("hello.php");
?>
When you run the wrapper.php, it will report
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in
/home/p/p6v8/public_html/hello.php on line 4

Here is a website which can tell you more about error_reporting setting. You could also change the error_reporting level by yourself.

Links

Congratulations! At this point, you have the basic information that you need to use PHP and to make it cooperate with Oracle. To find more information about using PHP, you can find many references on the web. Here are some that you may find particularly useful:

Contact TA Lauren Fung (w4y7@ugrad.cs.ubc.ca) for help if you have any questions.