CoE Home | computing | web |

Use PHP In your ENGR web pages

PHP is a popular server-side, cross-platform, HTML embedded scripting language. To use PHP scripting from a College of Engineering user account, you will need to have two things: a world readable public_html folder in your home directory and a world readable text file called .htaccess at the root level of your public_html folder. The file, .htaccess, will contain some special instructions for the COE webserver that allow your php code to be properly executed.

Caution: If you try to setup this up using windows tools (notepad, z drive, mounted public_html folder) it is almost guaranteed you will have problems. It is best to do the setup as instructed from UNIX. Afterwards, you should be able to write your code using Windows tools and editors, just be careful that the OS isn't adding unwanted extensions to your filenames.

If you have not setup a public_html folder, please read this document for instructions.

The steps you will need to take:

1. Logon to an ENGR host (flop.engr.orst.edu for example). If you do not know how to start a terminal session please read this document.

2. Once in your home directory, execute the following commands:

 

flop 1% cd public_html/

flop 2% pico .htaccess

3. Inside .htaccess, type the following lines substituting your username for [login], note that only the first block is neccessary:

 

Action php-script /cgi-bin/cgiwrap/~[login]/php
AddHandler php-script .php
#the above block tells the server to send files that end
# in .php to the php cgi for parsing
# PHP 5 is the default version.
# If you'd like to use PHP 4 instead, change the php at the end to php4

Action php-source /cgi-bin/cgiwrap/~[login]/php
AddHandler php-source .phps
#The above block tells the php cgi to display files ending
#in .phps as sytax-colored php code with errors indicated

Action php-scriptd /cgi-bin/cgiwrapd/~[login]/php
AddHandler php-scriptd .phpd
#The above block tells the server to run files through cgiwrapd
#for additional debugging info

4. Hit the keys "control-o" to save the document, then hit "control-x" to exit pico.

5. This document needs to be readable by the server, so type:

 

flop 4% chmod a+r .htaccess

6. Now, to verify that PHP is working for you, we will create a test file:

 

flop 9% pico test.php

7. Inside 'test.php' type the following lines:

<HTML>
<HEAD>
<TITLE>My first PHP script</TITLE>
</HEAD>
<BODY>



<?php



/* the above "<?php" signals that the PHP script has begun */

$today = date("Y-m-d");



PRINT "<CENTER>Today is: $today.</CENTER>";



/* the following "?>" closes the script */



?>



</BODY>
</HTML>

 

8. Save the file by hitting 'control-o' on your keyboard (remember to make sure you have the proper permissions: -rw-r--r--).

9. Launch your web browser and go to: (http://www.engr.orst.edu/~your-username-here/test.php). If you have followed the above instructions carefully, the page you load should display the current date.

Some things to remember:

  1. Error reporting is off by default for the php cgi. To see error messages in your scripts, include the line "<?php ini_set('display_errors', 'On'); ?>" at the top of your script or page. Runtime directives for php have no effect when placed in .htaccess files under the cgi environment.
  2. PHP statements must be within the "<?php...?>" brackets.
  3. Comments are enclosed within "/*" and "*/" brackets, or by placing a "#" sign at the beginning of the line.
  4. Statements that are to be outputted to the screen must be enclosed in double quotations ("), and prefaced by the PRINT statement.
  5. Almost every PHP command must end in a semi-colon (;).
  6. Any HTML commands placed within the PRINT statements will be interpreted by the browser, and perform their usual actions.
  7. Documents including PHP statements must be saved with the extension *.php (For example, myphpfile.php). This tells the PHP interpreter to execute any commands found within the script. (Actually the extension used can be adjusted or new extensions can be added as indicated in the .htaccess example at the top of this document.
  8. Date command:
    Syntax: string date(string format, int timestamp);

    The function can take two variables (timestamp) is optional. If supplied with a timestamp, the function returns a string containing a date formatted according to the parameters within the format string. In the above example, Y-m-d signifies year, month, and day, respectively, all in numerical format. There are many other format characters that can be placed within the format string. For a complete list, click here.

 

Links to resources for learning PHP can be found at php.net.

A particularly good beginners tutorial is over at W3Schools.

 

Related Articles:

Using .htaccess
Use SQLite with PHP
Setup a personal wiki
Make your PHP scripts ready for PHP 5

Article Information:

Date Created: Aug 22, 2003
Last Modified: Wed, Oct 29, 2008 9:20 AM
Views: 13016