Next Previous Contents

22. PHP Hypertext Preprocessor - Server-side html-embedded scripting language for PostgreSQL

WWW Interface Tool is at -

PHP also has a compiler called Zend which will vastly improve the performance. First you will write your application in PHP scripting language during development, testing and debugging. Once the project is ready for deployment you will use the Zend compiler to compile the PHP to create executable which will run very fast.

Old name is Professional Home Pages (PHP) and new name is PHP Hypertext Pre-Processor

PHP is a server-side html-embedded scripting language. It lets you write simple scripts right in your .HTML files much like JavaScript does, except, unlike JavaScript PHP is not browser-dependant. JavaScript is a client-side html-embedded language while PHP is a server-side language. PHP is similar in concept to Netscape's LiveWire Pro product. If you like free fast-moving software that comes with full source code you will probably like PHP.

22.1 Major Features

22.2 PHP - Brief History

PHP began life as a simple little cgi wrapper written in Perl. The name of this first package was Personal Home Page Tools, which later became Personal Home Page Construction Kit.

A tool was written to easily embed SQL queries into web pages. It was basically another CGI wrapper that parsed SQL queries and made it easy to create forms and tables based on these queries. This tool was named FI (Form Interpreter).

PHP/FI version 2.0 is a complete rewrite of these two packages combined into a single program. It evolved to a simple programming language embedded inside HTML files. PHP eliminates the need for numerous small Perl cgi programs by allowing you to place simple scripts directly in your HTML files. This speeds up the overall performance of your web pages since the overhead of forking Perl several times has been eliminated. It also makes it easier to manage large web sites by placing all components of a web page in a single html file. By including support for various databases, it also makes it trivial to develop database enabled web pages. Many people find the embedded nature much easier to deal with than trying to create separate HTML and CGI files.

Now PHP/FI is renamed as PHP.

22.3 So, what can I do with PHP ?

The first thing you will notice if you run a page through PHP is that it adds a footer with information about the number of times your page has been accessed (if you have compiled access logging into the binary). This is just a very small part of what PHP can do for you. It serves another very important role as a form interpreter cgi, hence the FI part of the old name. For example, if you create a form on one of your web pages, you need something to process the information on that form. Even if you just want to pass the information to another web page, you will have to have a cgi program do this for you. PHP makes it extremely easy to take form data and do things with it.

22.4 A simple example

Suppose you have a form:

<FORM ACTION="/cgi-bin/php.cgi/~userid/display.html" METHOD=POST> 
<INPUT TYPE="text" name="name"> 
<INPUT TYPE="text" name="age"> 
<INPUT TYPE="submit"> 
<FORM> 
Your display.html file could then contain something like:
< ?echo "Hi $ name, you are $ age years old!<p>" >
It's that simple! PHP automatically creates a variable for each form input field in your form. You can then use these variables in the ACTION URL file.

The next step once you have figured out how to use variables is to start playing with some logical flow tags in your pages. For example, if you wanted to display different messages based on something the user inputs, you would use if/else logic. In our above example, we can display different things based on the age the user entered by changing our display.html to:

<?
    if($age>50);
        echo "Hi $name, you are ancient!<p>";
    elseif($age>30);
        echo "Hi $name, you are very old!<p>";
    else;
        echo "Hi $name.";
    endif;
>
PHP provides a very powerful scripting language which will do much more than what the above simple example demonstrates. See the section on the PHP Script Language for more information.

You can also use PHP to configure who is allowed to access your pages. This is done using a built-in configuration screen. With this you could for example specify that only people from certain domains would be allowed to see your pages, or you could create a rule which would password protect certain pages. See the Access Control section for more details.

PHP is also capable of receiving file uploads from any RFC-1867 compliant web browser. This feature lets people upload both text and binary files. With PHP's access control and logical functions, you have full control over who is allowed to upload and what is to be done with the file once it has been uploaded. See the File Upload section for more details.

PHP has support for the PostgreSQL database package. It supports embedded SQL queries in your .HTML files.

PHP also has support for the mysql database package. It supports embedded SQL queries in your .HTML files.

22.5 CGI Redirection

Apache 1.0.x Notes

A good way to run PHP is by using a cgi redirection module with the Apache server. Please note that you do not need to worry about redirection modules if you are using the Apache module version of PHP. There are two of these redirection modules available. One is developed by Dave Andersen

and it is available at and the other comes bundled with Apache and is called mod_actions.c. The modules are extremely similar. They differ slightly in their usage. Both have been tested and both work with PHP.

Check the Apache documentation on how to add a module. Generally you add the module name to a file called Configuration. The line to be added if you want to use the mod_actions module is:

Module action_module mod_actions.o

If you are using the mod_cgi_redirect.c module add this line:

Module cgi_redirect_module mod_cgi_redirect.o

Then compile your httpd and install it. To configure the cgi redirection you need to either create a new mime type in your mime.types file or you can use the AddType command in your srm.conf file to add the mime type. The mime type to be added should be something like this:

     application/x-httpd-php phtml
If you are using the mod_actions.c module you need to add the following line to your srm.conf file:
     Action application/x-httpd-php /cgi-bin/php.cgi
If you are using mod_cgi_redirect.c you should add this line to srm.conf:
     CgiRedirect application/x-httpd-php /cgi-bin/php.cgi
Don't try to use both mod_actions.c and mod_cgi_redirect.c at the same time.

Once you have one of these cgi redirection modules installed and configured correctly, you will be able to specify that you want a file parsed by PHP simply by making the file's extension .phtml. Furthermore, if you add index.phtml to your DirectoryIndex configuration line in your srm.conf file then the top-level page in a directory will be automatically parsed by php if your index file is called index.phtml.

Netscape HTTPD

You can automatically redirect requests for files with a given extension to be handled by PHP by using the Netscape Server CGI Redirection module. This module is available in the File Archives on the PHP Home Page. The README in the package explicitly explains how to configure it for use with PHP.

NCSA HTTPD

NCSA does not currently support modules, so in order to do cgi redirection with this server you need to modify your server source code. A patch to do this with NCSA 1.5 is available in the PHP file archives.

22.6 Running PHP from the command line

If you build the CGI version of PHP, you can use it from the command line simply typing: php.cgi filename where filename is the file you want to parse. You can also create standalone PHP scripts by making the first line of your script look something like:

    #!/usr/local/bin/php.cgi -q
The "-q" suppresses the printing of the HTTP headers. You can leave off this option if you like.

22.7 PHPGem package

PHPGem is a PHP-script which accelerates the creation of PHP-scripts for working with tables. It works with different SQL-servers such as PostgreSQL, MySQL, mSQL, ODBC, and Adabas. You input a description of and parameters for your tables' fields (field name, on/off searching in the field, etc.), and PHPGem outputs another PHP-script which will work with the tables (view/add/edit/delete/duplicate entries and search). PHPGem works with multi-level nested tables. PHPGem allows you to specify a level of access for each table and for each field for each user. PHPGem also support images.

PHPGem is at http://sptl.org/phpgem


Next Previous Contents