PHP Introduction
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 on the students.cs.ubc.ca machines using the configurations that we prepared for you.
Setting Up public_html
This is not mandatory but we do recommend setting up an SSH key to help reduce the need for constantly entering in your password when SSH-ing into the department's servers.
The undergraduate webserver already has the Apache web server running. We can use it directly, and we don't need to set up a web server ourselves. What we need to do is set up our public_html directory on the undergrad server so we can access webpages under our name. Once we do so, we can access webpages in public_html by accessing https://www.students.cs.ubc.ca/~CWLusername. If you have previously created public_html in your account, then skip ahead to the next section.
- SSH into the undergrad servers.
ssh CWLusername@remote.students.cs.ubc.ca - Create a directory called "public_html".
mkdir ~/public_html - Change the file permissions for this directory to give users execute permission.
chmod 711 ~/public_html
The default serving webpages are in $CWL/public_html. Note: $CWL refers to your CWL username. For example, if your CWL username is "platypus", then the default serving webpages should be in platypus/public_html.
Necessary information about how to change file permissions has been consolidated below. MyCS has more information about setting up a personal website on the department's servers. If you want to learn more about Unix file permissions and sharing, visit this website (Undergrad -> Unix/Linux) to learn more.
Setting up PHP
Step 1: Putting a PHP file into Your public_html Directory
Let's put a PHP file into your public_html directory. You can do this one of two ways.
Method 1:
- SSH into the undergrad server.
- Run the following command (feel free to replace pico with nano, vi, vim, emacs, or any text editor of your choice).
pico ~/public_html/hello.php - Paste or type the following into the editor.
<html> <p>If PHP is working, you will see "Hello World" below, followed by some PHP version information:</p><hr /> <?php echo "Hello world"; phpinfo(); // Print PHP version and config info ?> </html> - Quit the editor by pressing control and x. Make sure you select Y to save your file.
Method 2:
- Download hello.txt.
- Rename hello.txt to hello.php.
- Transfer hello.php from your computer to public_html.
scp <location of the file>/hello.php CWLusername@remote.students.cs.ubc.ca:<location you want to transfer the file to>
Step 2: Setting File Permissions
Regardless of which method you choose, after hello.php exists in your public_html folder, you will have to give the file the correct permissions. For those who do not understand *nix file permissions, here are the recommended settings:
-
chmod 711 ~- This makes it so that people/the web server can read and change into your home directory. Note that if you care about the permissions on other files/directories in your home directory, you will need to set their permissions separately using the resources below. -
chmod 711 ~/public_html- This makes it so that people/the web server can read and change into your public_html directory. chmod 711 ~/public_html/hello.php- This makes it so that people/the web server can read and execute your php file. Note that you will need to do this for each file that you want to have served up by the web server.
If you do not take these steps, not only might it not work, but we will not be able to help you if things go wrong.
Note that depending on which methods you use to upload/change your files, you may need to reset the file permissions after uploading/editing.
If you want to learn more about Unix file permissions and sharing, visit this website (Undergrad -> Unix/Linux) to learn more.
Now, go to your browser and visit https://www.students.cs.ubc.ca/~CWLusername/hello.php . If everything works correctly, you will see "Hello world" and some information about PHP.
Getting PHP to Access Oracle
Here is a file for you to test your Oracle access.
- In the file that you have downloaded, replace "Username" and "Password" with your Oracle username and password. For example, if your CWL username was platypus and your student number was 12345678, then you would have
$c=OCILogon("ora_platypus", "a12345678", "dbhost.students.cs.ubc.ca:1522/stu"). - Follow the instructions in the Setting Up PHP section to create/transfer the file over to public_html.
- Ensure that the file has a .php extension and the correct file permissions. You can use
ls -la ~/public_htmlto double check these two things. - Go to your browser and launch https://www.students.cs.ubc.ca/~CWLusername/test.php . If you see a successfully connected message displayed in the browser, then you have successfully connected to Oracle using PHP.
Run a Sample Project
To help familiarize you with using PHP functions to manipulate your database, we have created a sample PHP file with examples and comments. Note: If you already have a table called "demoTable" in your Oracle database, it will be destroyed when running this program.
To get this file to work, follow the instructions in the Setting Up PHP section to transfer/create the file to/in public_html.
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 file.
Here is one possible solution to report PHP syntax errors and warning. For example, suppose that you want to check hello.php to see if it is right (spoiler: it's not).
<?php
echo "Hello world foo"
s
?>
You could write a PHP file (e.g., wrapper.php) to report any errors. It could look something like this:
<?php
error_reporting(-1);
ini_set('display_errors',1);
include("hello.php");
?>
When you run wrapper.php, it will report:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in <location of file>/hello.php on line 4
Here is a website which can tell you more about the error_reporting setting. You could also change the error_reporting level yourself.
Other Resources
Here are some links that may be helpful to you but this list is, by no means, exhaustive.