CISE Help & Resources
Running PHP Programs
Please note: This document asssumes that you know how to use Unix, understand how to create web pages at CISE, and that you have sufficient knowledge of how to write PHP programs.
For more information on PHP see php.net.
Hello World example
Suppose you have the following program located in $HOME/public_html/hello.php
#!/usr/local/bin/php <html><head><title>PHP Test</title></head> <body> <?php print( "Hello World<br />"); ?> </body></html>
and you wish to execute it. First you need to tell the webserver that files with an extension of .php are programs, and should be executed by the program specified in the shebang line. Create (or edit) a file called htaccess in the same directory as your script (or above it), and add the following lines:
AddHandler cgi-script .php DirectoryIndex index.html index.php
Both the program and the htaccess file should have the proper permissions, world executable for the former, world readable for latter. Neither should be world writable.
This can be done by running the commands:
chmod 711 hello.php chmod 644 htaccess
You should now be able to run the PHP program with this url:
http://www.cise.ufl.edu/~username/hello.php
And it should display "hello world" if everything went okay.
Debugging PHP Scripts
One of the most helpful tools for debugging PHP scripts is the interpreter's
native error reporting functionality. This option is disabled by default to
prevent security risks, but it can be enabled on a per-directory basis by
placing a customized php.ini file in that directory. Every
time a PHP script is run, it checks for this file in the local directory and
sets a number of configuration variables from its contents. By setting the display_errors variable
to On, you are configuring PHP to echo errors to the browser.
To enable error reporting for your application, follow the steps below:
- Download a modified version of the php.ini file here.
- Place this file in the directory containing your PHP scripts.
- Modify the file contents to your specifications (optional). The file you
downloaded is set to display all errors, but you may wish to further refine
the level of error reporting. Below is a list of global configuration variables
you may modify, with the suggested value in italics and corresponding line
number in parentheses.
(285) error_reporting = E_ALL (292) display_errors = On (297) display_startup_errors = Off (302) log_errors = On (345) error_log = ~username/public_html/php.error.log
- Debug your program. Navigate your browser to the page you wish to debug
on the testing server, www-pub.
http://www-pub.cise.ufl.edu/~username/foo.php
Any errors should now be displayed in the browser. - After debugging, set
display_errors = Offor remove thephp.inifile to prevent any security vulnerabilities.
For more information on using the php.ini file, see the file's verbose commenting or view the official online documentation.
Some Things to Keep in Mind
- The shebang line (#!/usr/local/bin/php) should be the first line of your file and there should be no whitespace preceding the hash or bang.
- Do NOT make the htaccess or any php file or your public_html directory world writeable. The webserver will (correctly) interpret this as a fatal error.
- The DirectoryIndex directive is not necessary, but useful. This will tell the webserver to look for index.html and then index.php as the default html files.
- Make sure the text file format for your script is Unix, not DOS.
See the commands dos2unix(1) and unix2dos(1) for more information - PHP scripts are run in the same way CGI scripts are, and therefore, much of the information about debugging CGI scripts applies to PHP programs as well. Make sure you are familiar with the section on debugging CGI programs.