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:
- Create a subdirectory called "public_html" in your account. Use the command mkdir ~/public_html. to generate a directory "public_html" in your account
- Users must have execute permission on this subdirectory. Use the command chmod 711 ~/public_html to change the file permission.
- Users must also have execute permission on their home directory. Use the command chmod 711 ~ to change the file permission.
Note: If you have created "public_html" in your account before, you don't need to create it again. Just make sure users have the
execute permissions on "public_html" and home directory.
- The default serving webpages are in $HOME/public_html. Note:$HOME refers to your login. If your unix login is "a1a1", then the default
serving webpages should be in a1a1/public_html
Now just put your PHP files in $HOME/public_html. Here is a php script file hello.txt for you to test. Rename it to have a ".php" extension like "hello.php"
Put the hello.php file in $HOME/public_html. For most files except C/C++ programs and other programs,
the file permission is "-rw-------" by default. Users must have execute permission on those PHP files. Use the command chmod 755 hello.php to change file permission.
To check if PHP web server is doing the right thing, run the following in a browser window:
http://www.ugrad.cs.ubc.ca/~account/hello.php
Note: account refers to your undergrad account. So if your unix login is "a1a1", then you should visit http://www.ugrad.cs.ubc.ca/~a1a1/hello.php.
You will see "Hello world foo" and some information about PHP if it works correctly.
Here is an detailed instruction public_html setup on how to create public_html in your home directory .
For those who want to learn more about unix file permissions and sharing, you could visit this website
Unix File Permissions And Sharing .
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.